如何调用可执行文件从MVC Web应用程序启动服务

时间:2023-01-19 15:28:15

I have a MVC4 app in which I need to init a long running process. Currently, the code for this process is in a console app being installed as a service with topshelf. I have the process checking a database every few seconds to see if it needs to be run, but that's not a solution. I need a way for the MVC4 app to kick off the process and forget about it, but the process NOT be unloaded with the web app when the response is returned to the client.

我有一个MVC4应用程序,我需要在其中初始化一个长时间运行的进程。目前,此过程的代码位于作为topshelf服务安装的控制台应用程序中。我让流程每隔几秒检查一次数据库,看看是否需要运行,但这不是解决方案。我需要一种方法让MVC4应用程序启动进程并忘记它,但是当响应返回给客户端时,不会使用Web应用程序卸载该进程。

Can someone point me in the right direction?

有人能指出我正确的方向吗?

1 个解决方案

#1


1  

If I'm understanding your question, what you can do is, in the service (the class that's derived from ServiceBase), override OnCustomCommand:

如果我理解你的问题,你可以做的是,在服务(从ServiceBase派生的类)中,重写OnCustomCommand:

private const int MY_CUSTOM_COMMAND = 140;
protected override void OnCustomCommand(int command)
{
    if (command == MY_CUSTOM_COMMAND)
    {
       ... Do stuff here ...
    }
}

You can then trigger the command in your service, from some external application along these lines:

然后,您可以从以下这些行的某些外部应用程序中触发服务中的命令:

private const int MY_CUSTOM_COMMAND = 140;
using (ServiceController sc = new ServiceController("MyTaskService", "ServiceMachine"))
{
   sc.ExecuteCommand(MY_CUSTOM_COMMAND);
}

That's the basic idea. Custom commands can be any value from 128-256 inclusive.

这是基本的想法。自定义命令可以是128-256(含)的任何值。

We use a similar system in our web app, which allows users to submit "jobs" that are then run by a windows service. The web app sends a command to the windows service to let it know a new job has been submitted. The service then goes to the DB to get the information about the job to execute.

我们在我们的网络应用程序中使用类似的系统,允许用户提交随后由Windows服务运行的“作业”。 Web应用程序向Windows服务发送命令,以使其知道已提交新作业。然后,该服务转到数据库以获取有关要执行的作业的信息。

#1


1  

If I'm understanding your question, what you can do is, in the service (the class that's derived from ServiceBase), override OnCustomCommand:

如果我理解你的问题,你可以做的是,在服务(从ServiceBase派生的类)中,重写OnCustomCommand:

private const int MY_CUSTOM_COMMAND = 140;
protected override void OnCustomCommand(int command)
{
    if (command == MY_CUSTOM_COMMAND)
    {
       ... Do stuff here ...
    }
}

You can then trigger the command in your service, from some external application along these lines:

然后,您可以从以下这些行的某些外部应用程序中触发服务中的命令:

private const int MY_CUSTOM_COMMAND = 140;
using (ServiceController sc = new ServiceController("MyTaskService", "ServiceMachine"))
{
   sc.ExecuteCommand(MY_CUSTOM_COMMAND);
}

That's the basic idea. Custom commands can be any value from 128-256 inclusive.

这是基本的想法。自定义命令可以是128-256(含)的任何值。

We use a similar system in our web app, which allows users to submit "jobs" that are then run by a windows service. The web app sends a command to the windows service to let it know a new job has been submitted. The service then goes to the DB to get the information about the job to execute.

我们在我们的网络应用程序中使用类似的系统,允许用户提交随后由Windows服务运行的“作业”。 Web应用程序向Windows服务发送命令,以使其知道已提交新作业。然后,该服务转到数据库以获取有关要执行的作业的信息。