c# - ASP.NET Web Api, Database connection in Threads -
i have issue using database in thread in asp.net application. when want start application want start thread called "backgroundworker" it, runs in background till whole application stopped.
the problem have massive problems dbcontext in thread.
i try start walker in startup.cs in methods "configureservices" or "configure" , initialize dbcontext in constructor in walker "dbcontext = new applicationcontext()" tells me connection not configured, when try operate in while(true) queue on database. if write own controller walker receives applicationcontext in constructor , starts thread this, if call controller once request:
public backgroundworker(chronicuscontext dbcontext) { _dbcontext = dbcontext; _messageservice = new mailmessageservice(); } // get: api/backgroundworker [httpget] [route("start")] public void startworker() { //thread thread = new thread(this.dobackgroundwork); thread thread = new thread(() => dobackgroundwork(this._dbcontext)); thread.start(); } public void dobackgroundwork(chronicuscontext _dbcontext) { while (true) { if (_dbcontext.pollmodels.any()) //here exception { ... } } }
then receive system.objectdisposedexception object disposed inside while (true) queue.
i tried , similar things in many different ways allways receive exceptions these 2 or database connection closed.
can me , tell me, how works?
thank you!
generally, server side multithreading web applications not happen , is, times, huge no no.
conceptually, server "multithreaded", handles many http requests clients/users/other servers. mobile , web architecture/design, server(s) process multiple requests , clients handling asynchronous calls , dealing waiting responses long running calls api method startworker
.
think of scenario, make request webapi method startworker
, client, making request waiting response, putting work on thread nothing client still waiting response.
for example, let's consider client html web page ajax call. call startworker
via ajax
, loading data html table. desire, ux perspective, put progress spinner while long running startworker
responds html page ajax call request. when startworker
responds, ajax call loads html table startworker
response. startworker
has respond data. if startworker
responds beforehand have send push notification, via signalr, example, when other thread completes , has data need html table.
hopefully, see, call webapi method, takes same amount of time ajax request/response perspective, multithreading becomes pointless in scenario, common web application scenario.
you can have client ui load other ui elements, showing progress spinner in html table ui area, until database call complete , responds data ajax call. way users know things happening , still loading.
if still need additional thread in api project needs, believe have using entity framework 6 or greater support asynchronous queries, see tutorial:
http://www.codeproject.com/tips/805923/asynchronous-programming-in-web-api-asp-net-mvc
update
now know need run sql query on repeating frequency of time, , have azure web app, want use azure automation if using sql azure or create sql server job if using sql server instance backend
Comments
Post a Comment