Monday 15 August 2011

asp.net mvc 4 - AsyncController used to run a console application -



asp.net mvc 4 - AsyncController used to run a console application -

there's few questions address async controller workings none quite deal needed. someone's done , can tell me if i'm doing wrong.

i've got combination of web application deals configuring tasks , console application counterpart deals running configured tasks. wanted able run tasks web application clicking button on command returned user , task executed in background. asynccontroller seems perfect match. both applications access same database using ef6, unity dependency injection , sql server 2012. web interface targeting .net 4.5 on mvc4.

i can run console application fine without hitch. can trigger run web interface using below code. problem when triggered through web application, task run point (i can see in logs (nlog)) stops executing until rebuild solution - solution contains both applications, replace .exe beingness run. don't pauses when run console application directly.

it's first time using task , i'm bit shy process class too. please don't harsh if i'm doing incredibly stupid.

here's code controller:

public class taskrunnercontroller : asynccontroller { private readonly itaskprocessservice _taskprocessservice; private readonly iauthenticationcontext _context; private readonly iservicebase<task> _taskservice; public taskrunnercontroller(itaskprocessservice taskprocessservice, iauthenticationcontext context, iservicebase<task> taskservice) { _taskprocessservice = taskprocessservice; _context = context; _taskservice = taskservice; } private string taskrunnerexe { { var setting = configurationmanager.appsettings["taskrunner.exe"]; guard.against<configurationerrorsexception>(string.isnullorempty(setting), "missing configuration setting: taskrunner.exe"); homecoming setting; } } [customauthorize(typeof(canrun))] public actionresult runtaskasync(long id) { var task = _taskservice.find(i => i.taskid == id); guard.againstload(task, id); var fileinfo = new fileinfo(taskrunnerexe); guard.against<configurationerrorsexception>(!fileinfo.exists, "taskrunner not found @ specified location: {0}", taskrunnerexe); var taskprocess = _taskprocessservice.schedule(task, _context.principal.identifier); asyncmanager.outstandingoperations.increment(); system.threading.tasks.task.factory.startnew(() => { processstartinfo info = new processstartinfo(fileinfo.fullname, string.format("{0} {1}", task.taskid, taskprocess.taskprocessid)); info.useshellexecute = false; info.redirectstandardinput = true; info.redirectstandardoutput = true; info.createnowindow = true; var process = new process { startinfo = info, enableraisingevents = true }; process.exited += (sender, e) => { asyncmanager.outstandingoperations.decrement(); }; process.start(); }); if (_context.principal.haspermission<canviewlist>()) homecoming redirecttoaction("index", "tasks"); homecoming redirecttoaction("index", "home"); } public actionresult runtaskprogress() { homecoming view(); } public actionresult runtaskcompleted() { homecoming content("completed", "text/plain"); } }

dependencies:

taskprocessservice: repository each event of task runs context: keeps info current user taskservice: repository tasks

the console application (.exe) exits when it's finished. mentioned, when invoked through web application, task finish when rebuild application - @ point should - seems working whole time, stopped logging or reporting @ point in process.

perhaps it's of import - had seek @ without async controller, same setup - run process, wrapped in task had same end effect. thought process beingness killed when main thread returned pool, that's why tried asynccontroller.

if open task manager can see process exe sits there idle. application logs it's progress point sits there. problem vs? i'm using vs2012.

am wrong wrap process task? there way run executable straight via task class , action class?

many insight.

more likely, application beingness terminated because worker process ends. iis terminate threads or kid processes running under requests thread after has finished.

as such, it's not viable you're doing. move console app schedule task, , trigger scheduled task web application.

asp.net-mvc-4 process console-application executable asynccontroller

No comments:

Post a Comment