简体   繁体   English

从我的asp.net mvc Web应用程序调用asp.net控制台应用程序

[英]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. 我使用visual studio 2012创建了一个asp.net控制台应用程序,控制台应用程序是我们的ERP系统和我们的自定义数据库之间的同步工作。 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. 现在我需要这个同步作业及时运行,所以我在我们的Windows任务调度程序下创建了一个新任务,它每小时调用一次控制台应用程序,效果很好。

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 ? 但我需要能够由最终用户手动运行控制台应用程序,主要是用户可以登录我们的网络应用程序(asp.net mvc),然后他们可以点击“同步”按钮,这里有“同步”按钮将主要调用控制台应用程序...这可能吗? i mean can i have my web application calls the console application , the same way the Task Schuler calls the console application ? 我的意思是我可以让我的Web应用程序调用控制台应用程序,就像Task Schuler调用控制台应用程序一样吗?

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 ? 第二个问题,如果我设法从我的asp.net mvc web应用程序调用控制台应用程序,这会强制控制台应用程序在IIS下运行吗? or the web application can call the console application outside the IIS scope ? 或者Web应用程序可以在IIS范围外调用控制台应用程序? similar to calling the console application from Task scheduler ? 类似于从任务调度程序调用控制台应用程序?

Question 1: Yes you can. 问题1:是的,你可以。

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. 问题2:该过程将在IIS帐户下运行,该帐户可能没有您的流程所需的权限。 You can impersonate another user before calling Process.Start(). 您可以在调用Process.Start()之前模拟其他用户。 See this example: 看这个例子:

Change user for running windows forms program 更改用户运行Windows窗体程序

You can create a web service( use asp.net web api ) and put your synchronization code inside that. 您可以创建一个Web服务( 使用asp.net web api )并将您的同步代码放在其中。 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. Web API可以托管在IIS中(或者您也可以执行非IIS托管),当用户单击UI中的同步按钮时,您可以从asp.net mvc应用程序访问此Web api端点。 You may use HttpClient class to make the Http call to the web api. 您可以使用HttpClient类对Web api进行Http调用。

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. 即:您可以使用HttpClient类对Web api端点进行http调用,该端点执行您的代码以同步您的数据。

While this answers your original question, It is not a good idea to run such a background task on an asp.net thread . 虽然这回答了你原来的问题,但在asp.net线程上运行这样的后台任务并不是一个好主意 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. App域可能会因为很多原因而随时关闭,它也会占用您长时间运行的任务。 But fortunately there are some elegant and simple to use solutions available. 但幸运的是,有一些优雅和简单易用的解决方案。

  1. HangFire 迟发型
  2. FluentScheduler FluentScheduler
  3. Quartz 石英

These libraries have been designed around some of the pitfalls of manually running some code in the background in ASP.NET. 这些库是围绕在ASP.NET中后台手动运行某些代码的一些缺陷而设计的。 Take a look at this blog post where scott explains how to use these libraries. 看一下这篇博客文章 ,其中scott解释了如何使用这些库。

您可以从Web应用程序执行任何命令,您可以在此处查看从C#代码运行exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM