在 MVC Web 应用程序上运行 TopShelf
我需要为我正在构建的 Web 应用程序执行一些后台任务。我喜欢使用 Topshelf 在服务器上运行服务的外观。我想知道初始化服务的最佳方法。我想知道从 MVC 应用程序启动中启动服务是否是一个坏主意
例如:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
InitService();
}
private void InitService()
{
Task.Factory.StartNew(() => HostFactory.Run(x =>
{
x.Service<TestService>(s =>
{
s.SetServiceName("TestService");
s.ConstructUsing(
name => new TestService());
s.WhenStarted(ts => ts.Start()); s.WhenStopped(ts => ts.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Sample test service");
x.SetDisplayName("Test display name");
x.SetServiceName("Test service name");
}));
}
显然启动应用程序的工作是在它自己的任务中运行的。 有更好的方法吗?你能看出这有什么问题吗?
我的想法是,一旦服务启动,它将从此开始自行处理。
我唯一的想法是:如果多次重新启动 Web 应用程序,但我想我可以放入一些逻辑来检查服务是否已经在运行,然后不要启动另一个服务。
I need to do some background tasks for a web application i'm building. I like the look of using Topshelf to run a service on the server. I was wondering the best way of initialising the service. I was wondering if it would be a bad idea to start the service from the MVC application start up
For example:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
InitService();
}
private void InitService()
{
Task.Factory.StartNew(() => HostFactory.Run(x =>
{
x.Service<TestService>(s =>
{
s.SetServiceName("TestService");
s.ConstructUsing(
name => new TestService());
s.WhenStarted(ts => ts.Start()); s.WhenStopped(ts => ts.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Sample test service");
x.SetDisplayName("Test display name");
x.SetServiceName("Test service name");
}));
}
obviously the job of starting the application is run in its own task.
Are there better ways of doing this? Could you see a problem with this?
My idea is that once the service is started, it will handle its self from then on.
My only thought was: if you restart your web application multiple times, but I thought I could just put some logic in to check that if the service is already running then don't start another one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可行的,但这使得该服务处于 Web 应用程序的范围内(在 IIS 内管理) - 您并没有真正将其安装为 Windows 服务。这意味着如果应用程序池下降,您的服务也会下降。
如果您想将该服务安装为 Windows 服务,则需要检查它是否存在,然后在控制台应用程序中运行“servicename install start”。这实际上取决于你想要什么。将其作为应用程序的一部分可能会给您带来一些好处,但如果您需要在多台计算机上运行该应用程序,则可能会很尴尬。
就我个人而言,我总是将其作为单独的服务运行,而不是在 IIS 内的 Web 应用程序范围内运行。
This works, but this makes the service live in the scope of the web app (managed inside IIS) - you aren't really installing it as a windows service. This means if the app pool drops, so does your service.
If you wanted to install the service as a windows service, you would need to check for it's existence, then run "servicename install start" with it inside a console app. It really depends on what you want. Having it part of the app might give you something but if you need to run the app on multiple machines it might be awkward.
Personally, I would always make it run as a separate service and not in the scope of the web app inside IIS.
您也可以使用 Quartz 执行此操作,无需担心
You can do this with Quartz without any concern too