Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an asp.net console application from my asp.net mvc web application

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 ?

like image 537
John John Avatar asked Jan 13 '16 01:01

John John


People also ask

Is ASP NET MVC cross platform?

The primary difference between ASP.NET MVC and ASP.NET Core is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC can only be used for applications on Windows.

How do I run a console application?

The single code statement calls the WriteLine method to display the literal string "Hello, World!" in the console window. If you press F5, you can run the default program in Debug mode. After the application runs in the debugger, the console window stays open. Press any key to close the console window.


2 Answers

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

like image 120
Frank Hagenson Avatar answered Sep 28 '22 11:09

Frank Hagenson


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.

like image 39
Shyju Avatar answered Sep 28 '22 11:09

Shyju