c# - Run stored procedure in a background Web Api -


in web api application need in topic: best way run background task in asp.net web app , feedback? in application user upload excel file , import data tables in database. works fine, import process can take long time (about 20 minutes, if excel have lot of rows) , when process started page blocked , users have wait time. need import process run in background.

i have controller post method:

[httppost] public async task<ihttpactionresult> importfile(int procid, int fileid) {     string importerrormessage = string.empty;      // excel file , read     string path = filepath(fileid);     datatable table = gettable(path);      // add record start process in table logs     using (var transaction = db.database.begintransaction()))     {         try         {             db.uspaddlog(fileid, "process started", "the process started");             transaction.commit();         }         catch (exception e)         {             transaction.rollback();             importerrormessage = e.message;             return badrequest(importerrormessage);         }     }      //using myhelper start store procedure, import data excel file     //the procedure add record in table logs when finished     using (myhelper helper = new myhelper())     helper.startimport(procid, fileid, table, ref importerrormessage);          if (!string.isnullorempty(importerrormessage))         return badrequest(importerrormessage);      return ok(true); } 

i have method, returns information file , process

[httpget] [responsetype(typeof(filedto))] public iqueryable<filedto> getfiles(int procid) {     return db.leadprocesscontrol.where(a => a.processid == procid)                                 .project().to<filedto>(); } 

it returns json this:

{   filename = name.xlsx   fileid = 23   processid = 11   status = started } 

this method grid

file name | status | button run import | button delete file 

this status table logs , in filedto placed last value, example if upload file status "file uploaded" when run import status "started" , when finished status "complete". page locked when import process running, status "complete".

so need run procedure in background , method should return new status if has been changed. suggestions?

adding async method don't make method call asynchronous. indicate thread handling current request can reused processing other requests while waiting network/disk io. when client call method response once method complete. in other word async sever side thing , nothing client call. need start long running process in separate thread shown below. best practice not use web app such long running processing instead long processing in separate windows service.

[httppost]     public async task<ihttpactionresult> importfile(int procid, int fileid) { string importerrormessage = string.empty;  // excel file , read string path = filepath(fileid); datatable table = gettable(path);  // add record start process in table logs using (var transaction = db.database.begintransaction())) {     try     {         db.uspaddlog(fileid, "process started", "the process started");         transaction.commit();     }     catch (exception e)     {         transaction.rollback();         importerrormessage = e.message;         return badrequest(importerrormessage);     } }           //start long running process in new thread          task.factory.startnew(()=>{           using (myhelper helper = new myhelper())         {            helper.startimport(procid, fileid, table, ref importerrormessage);            //** code running background thread cannot return here. need store status in database.            //if (!string.isnullorempty(importerrormessage))          //return badrequest(importerrormessage);        }          });  //you return ok indicate background process started return ok(true); } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -