Calling an asp.net console application from my asp.net mvc web application
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
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.
- HangFire
- FluentScheduler
- 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.