6

I have created an asp.net console application using visual studio 2012, the console application is a sync job between our ERP system and our custom Database. now i need this sync job to run on timely basis , so i created a new task under our windows task scheduler , which calls the console application each hour, which works well.

but i need to have the ability to run the console application manually by end users,, mainly users can login to our web application (asp.net mvc) and from there they can click on "Sync" button , where this "Sync" button will mainly calls the console application... so is this possible ? i mean can i have my web application calls the console application , the same way the Task Schuler calls the console application ?

second question, if I manage to call the console application from my asp.net mvc web application,, will this force the console application to run under IIS ? or the web application can call the console application outside the IIS scope ? similar to calling the console application from Task scheduler ?

4 Answers4

7

Question 1: Yes you can.

public ActionResult Index()
{
    Process.Start(@"c:\Windows\System32\cmd.exe");
    return View();
}

Question2: The process will run under the IIS account, which may not have the privileges your process needs. You can impersonate another user before calling Process.Start(). See this example:

Change user for running windows forms program

Community
  • 1
  • 1
Frank Hagenson
  • 953
  • 8
  • 17
  • when I am saying under IIS scope I do not mean permission,, what I am asking about is as follow:- now let say my web application calls the console application and the console application start running , and during the console application execution the application pool restart,, will this cause the console application to restart also ..or it will not get affected. –  Jan 13 '16 at 02:09
  • 1
    Oh I see. I'm pretty sure it will continue. In Windows killing the parent process generally has no effect on child processes that are not communicating with the parent. – Frank Hagenson Jan 13 '16 at 02:13
  • do not worry maybe I did not explain my question.. so you mean I can have my asp.net mvc calls my console application , in the same way as the task schdulerer calls the console application. of course using tasks scheduler will call it on timly basis , while from the web application will call it manually by end users... –  Jan 13 '16 at 02:15
  • can you provide some links which shows how I can call a console application from my asp.net mvc ? –  Jan 13 '16 at 02:18
  • 3
    public ActionResult Index() { Process.Start(@"c:\Windows\System32\cmd.exe"); return View(); } I just tried this and the process stays running even when you stop IIS – Frank Hagenson Jan 13 '16 at 02:47
  • Just note that this example is limited. It answers the question at hand but if you need to specify window properties or redirect output and errors then there is a host of options that you would need to manually construct within the Process object or ProcessStartInfo object then assign it to the Process. Also many people are going to run into permissions issues trying to start a Process from ASP.NET which leads to the topic of impersonation etc., Also depending on how long the process runs it would be good to research running this in an Async fashion and include some wait time error handling. – DtechNet Sep 11 '17 at 22:26
3

You can create a web service(use asp.net web api) and put your synchronization code inside that. The Web API can be hosted in IIS ( or you can do a non IIS hosting as well) and you can access this web api endpoint from your asp.net mvc application when user clicks on the sync button in the UI. You may use HttpClient class to make the Http call to the web api.

Now from your console application, which is being invoked from the task scheduler, you can do the same. ie: you can use HttpClient class to make an http call to the web api endpoint which executes your code to sync your data.

While this answers your original question, It is not a good idea to run such a background task on an asp.net thread. The App domain can go down at any time for a lot of reasons and it will take down your long running task as well. But fortunately there are some elegant and simple to use solutions available.

  1. HangFire
  2. FluentScheduler
  3. Quartz

These libraries have been designed around some of the pitfalls of manually running some code in the background in ASP.NET. Take a look at this blog post where scott explains how to use these libraries.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • but it is not recommended to have background jobs such as my sync job to run under IIS .. That why I chose to have the sync job as a console application,, but I want to have the ability for this console application to be manually executed by end users from our web application, in addition to being executed on timely basis using task schdulerer.... –  Jan 13 '16 at 02:12
  • now there are lot of discussion that can go here,, now I used HangFire previously,, but at the end IIS is not designed to run background jobs ,, so that why I chose to have it as a console application ,, but I want to know if I can call this console application from my asp.net mvc ... –  Jan 13 '16 at 02:23
  • also one thing I faced when using hangfire to run background jobs, is that if there is not any activity on the web site, the background job will not run, till the first user access the application,, unlike having the background job runs as a task inside the task schdulerer ... while will always run as scheduled –  Jan 13 '16 at 02:28
  • You can keep both options(task scheduler and web api) and you can keep a table record to track whether your activity completed successfully.If not run again. – Shyju Jan 13 '16 at 02:30
  • so you are trying to say that using tools such as hangfire to run background jobs is a valid and robust approach,,, I remember that I wrote a question on msdn where I mentioned that I will be running a sync job as an action method inside my asp.net mvc web application and I will call it fromhangfire,, and I recvied around 4 replies that I should not use this approach and I need to have the sync job as a console application,,, so I am confused again .. –  Jan 13 '16 at 02:32
  • just to keep my question clear, I am asking if I can have my web application calls the console application or not ? –  Jan 13 '16 at 02:34
  • 1
    Any solution can go down :) Console job executed from your task scheduler seems to be a robust option to me. Also IF you are very particular about calling your console app from your other c# code(mvc app). Take a look at http://www.dotnetperls.com/process – Shyju Jan 13 '16 at 02:36
  • ok so seems it is possible to call a console application from mvc using Process.Start , but if my asp.net mvc calls the console application , and during the console application execution the application pool restart or went down will this cause the console application to stop ? or once the console application start executing using Process.Start there will not be any link with the web application ? –  Jan 13 '16 at 02:46
  • Using Process, It is a Fire- and Forget i believe. – Shyju Jan 13 '16 at 02:48
  • 1
    Do you know something that choosing how to run background jobs is the most confusing topic I have ever faced,, As many people say that IIS is not designed to run background jobs in general and specifically long running background jobs . but on the other side there are many tools such as hangfire which is designed to run background jobs under iis and many articles support these tools http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx ,, But also you can find many people are against these tools also ... so not sure which opinion is the right one to follow? –  Jan 13 '16 at 03:06
  • IMHO, Use the simplest working solution for your needs. when you run into situations where your current solution is not handling your load, Step up to the next one. There is no point in bringing a big and complex system for your small use case **BECAUSE PEOPLE SAY IT IS GOOD** – Shyju Jan 13 '16 at 03:11
0

create a web api which communicates to a windows service (possibly using grpc). user clicks button --> request hits web api endpoint --> grpc call to windows service (grpc server) --> windows service runs the application

-1

You can execute any command from your web application you can look here Run an exe from C# code

Community
  • 1
  • 1
  • but will this command runs under IIS scope as I want to avoid having my sync job runs under IIS scope ? –  Jan 13 '16 at 02:03