使用Topshelf创建的Windows服务会自动停止 [英] Windows Service created using Topshelf is stopped automatically

查看:206
本文介绍了使用Topshelf创建的Windows服务会自动停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Topshelf创建了Windows服务. 在Start()中,我配置了在控制台应用程序中创建的SelfHosted WebAPI,如下所示

:

公共无效Start()
        {
            尝试
            {
                baseUri ="http://localhost:8080&";

                Console.WriteLine(正在启动Web服务器...");
                
                var config = new HttpSelfHostConfiguration(baseUri);
                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute(
                "DefaultApi",
                " api/{controller}/{id}",
                新的{id = RouteParameter.Optional});

                使用(HttpSelfHostServer服务器=新的HttpSelfHostServer(config))
                {
                    Console.WriteLine(服务器在{0}运行-",baseUri);

                    server.OpenAsync().Wait();
                    Console.WriteLine(按Enter退出.");
                    Console.ReadLine();
                }

                Console.WriteLine(按Enter退出.");
                Console.ReadLine();
            } 

我在ConfigureService类中将其配置为

尝试
            {
                HostFactory.Run(configure =>
                {
                    configure.Service< WebServer>(service =>
                    {
                        service.ConstructUsing(s => new WebServer());
                        service.WhenStarted(s => s.Start());
                        service.WhenStopped(s => s.Stop());
                    });

                    //窗口服务用来运行Process Engine的设置帐户.
                    configure.RunAsLocalService();
                    configure.StartAutomatically();
                    configure.SetServiceName("Engine");
                    configure.SetDisplayName("Engine");
                    configure.SetDescription(引擎Windows服务");
                });
            } 

最后是我在控制台应用程序的main()中调用

 ConfigureService.Configure();

                Console.ReadLine(); 

现在,当我调试此应用程序时,一切正常.服务启动,我可以点击指定的URL.但是,当我使用命令行安装此服务时,该服务会启动,但是当我向url发送请求时,该服务会自动停止.

我在事件日志中发现了以下错误消息

引擎服务终止,并出现以下错误:
没有足够的存储空间来处理此命令.

我找到了一些在线解决方案,并将IRPStackSize的大小增加到最大(十进制为80),但对我不起作用.请帮忙.

解决方案

Jitendrs,

感谢您的信息.

根据您的描述,您正试图通过Topshelf在Windows Service中托管Web API,对吗?

我们不熟悉Topshelf,因为与Topshelf有关的问题,建议您在下面打开一个新问题.

https://github.com/Topshelf/Topshelf

要在Windows服务中托管Web api,建议您参考以下链接以托管Web api.

#Self-Host ASP.NET Web API 1(C#)

https://docs .microsoft.com/en-us/aspnet/web-api/overview/older-versions/self-host-a-web-api

对于创建Windows服务,建议您参考以下链接.

#演练:在组件设计器中创建Windows Service应用程序

https://msdn.microsoft.com/en-us/library/zt39148a(v = vs.100).aspx

最好的问候,

爱德华


I have created a Windows Service using Topshelf. In the Start() I have configured a SelfHosted WebAPI created in a Console application as follows 

public void Start()
        {
            try
            {
                baseUri = "http://localhost:8080";

                Console.WriteLine("Starting web Server...");
                
                var config = new HttpSelfHostConfiguration(baseUri);
                	config.MapHttpAttributeRoutes();
                	config.Routes.MapHttpRoute(
                	    "DefaultApi",
                	    "api/{controller}/{id}",
                	    new { id = RouteParameter.Optional });

                using (HttpSelfHostServer server = new HttpSelfHostServer(config))
                {
                    Console.WriteLine("Server running at {0} - ", baseUri);

                    server.OpenAsync().Wait();
                    Console.WriteLine("Press Enter to quit.");
                    Console.ReadLine();
                }

                Console.WriteLine("Press Enter to quit.");
                Console.ReadLine();
            }

I configured this in my ConfigureService class as 

            try
            {
                HostFactory.Run(configure =>
                {
                    configure.Service<WebServer>(service =>
                    {
                        service.ConstructUsing(s => new WebServer());
                        service.WhenStarted(s => s.Start());
                        service.WhenStopped(s => s.Stop());
                    });

                    //Setup Account that window service use to run Process Engine.  
                    configure.RunAsLocalService();
                    configure.StartAutomatically();
                    configure.SetServiceName("Engine");
                    configure.SetDisplayName("Engine");
                    configure.SetDescription("Engine Windows Service");
                });
            }

and finally in the main() of the console application I am calling 

                ConfigureService.Configure(); 

                Console.ReadLine();

Now when I debug this application everything works fine. The service starts and the I can hit the url specified. But when I install this service using command line, the service starts but when I send a request to the url the service stops automatically. 

I found the following error message in the event logs 

The Engine service terminated with the following error: 
Not enough storage is available to process this command.

I found some online solutions for this and increased the size of IRPStackSize to it's maximum that is 80 in decimal but didn't work for me. Please help. 

解决方案

Hi Jitendrs,

Thanks for your post.

Based on your description, you are trying to host Web API in Windows Service by Topshelf, am I right?

We are not familiar with Topshelf, for the issue related with Topshelf, I would suggest you open a new issue in below.

https://github.com/Topshelf/Topshelf

For hosting web api in windows service, I would suggest you refer below link for self-host web api.

#Self-Host ASP.NET Web API 1 (C#)

https://docs.microsoft.com/en-us/aspnet/web-api/overview/older-versions/self-host-a-web-api

For creating Windows Service, I suggest you refer below link.

#Walkthrough: Creating a Windows Service Application in the Component Designer

https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.100).aspx

Best Regards,

Edward 


这篇关于使用Topshelf创建的Windows服务会自动停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆