c# - How do I run Background Tasks in ASP.NET Web.Api? -
i'm developing asp.net mvc web api 2 app c# , .net framework 4.5.1.
sometimes, when user calls on apicontroller
have run task in background. task fill database table more data.
searching on internet have found article, how run background tasks in asp.net. , want use hangfire. reading documentation haven't found way use without using owin on asp.net web api app (i'm not using right now).
is there way use hangfire without owin? or maybe, there option run background tasks on asp.net web api 2 app?
i'm not sure if still need (i doubt it) i'll answer question since ranking high on google search. well, conventional way start hangfire server using owin follows...
public void configuration(iappbuilder app){ app.usehangfireserver(); }
server lifecycle
however, based on documentation...hangfire not reliant on owin @ , need start , stop backgroundjobserver
(source) http://docs.hangfire.io/en/latest/background-processing/processing-background-jobs.html
hangfire server part responsible background job processing. server not depend on asp.net , can started anywhere, console application microsoft azure worker role. single api applications exposed through backgroundjobserver class
that means start server when application starts...
var server = new backgroundjobserver();
and dispose when application ends...
server.dispose();
and that's app.usehangfireserver();
does. see source code...
https://github.com/hangfireio/hangfire/blob/master/src/hangfire.core/appbuilderextensions.cs#l293
and
https://github.com/hangfireio/hangfire/blob/master/src/hangfire.core/appbuilderextensions.cs#l311
see example below using global.asax..
backgroundjobserver _server; protected void application_start(object sender, eventargs e) { globalconfiguration.configuration .usesqlserverstorage("your_connection_string"); _server = new backgroundjobserver(); } protected void application_end(object sender, eventargs e) { _server.dispose(); }
sql server storage
to register sql server storage don't need owin, need do...
hangfire.globalconfiguration.configuration.usesqlserverstorage("your_connection_string");
again, came conclusion looking @ source code here...
conclusion
hangfire not rely on owin , have acknowledged
Comments
Post a Comment