使用OWIN将Web API托管为Windows服务 [英] Host Web API as Windows Service using OWIN

查看:103
本文介绍了使用OWIN将Web API托管为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OWIN将Web API应用程序作为Windows服务运行.但是,在尝试启动服务时收到以下消息:

本地计算机上的[ServiceName]服务已启动,然后停止.如果某些服务未被其他服务或程序使用,则会自动停止.

由于某种原因,我的服务无法理解它应该继续监听 http://localhost:9000

VS解决方案由两个项目组成:Web API和Windows服务.

在Windows服务项目中,我有:

static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}

public partial class Service : ServiceBase
{
    private const string _baseAddress = "http://localhost:9000/";
    private IDisposable _server = null;

    public Service()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        _server = WebApp.Start<Startup>(url: _baseAddress);
    }

    protected override void OnStop()
    {
        if (_server != null)
        {
            _server.Dispose();
        }
        base.OnStop();
    }
}

OnStart 中, Startup 引用Web API项目中的 Startup 类:

public class Startup
{
    public void Configuration(IAppBuilder builder)
    {
        var config = new HttpConfiguration();        
        config.Routes.MapHttpRoute("Default",
            "{controller}/{id}",
            new { id = RouteParameter.Optional });
        builder.UseWebApi(config);
    }
}

(它还包含一些有关Unity和日志记录的配置.)

我想Windows服务项目的安装程序部分基本上无关紧要,尽管知道我有以下内容可能会很有用:

this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;

我尝试过的事情:

  • 要使用控制台应用程序托管Web API,然后将控制台应用程序作为Windows Service托管.但是,即使我包含了'Console.ReadKey()',它也只是停止了.
  • 要遵循本指南,请执行以下操作: OWIN-WebAPI-Service 奇怪的是我可以让他的服务正常工作,但是当我尝试更改代码以匹配他的设置时,我一直遇到相同的错误.

完整的源代码: github.com/SabrinaMH/RoundTheClock-Backend/tree/exploring_hosting

解决方案

当您获取启动然后停止本地计算机上的服务"时,通常意味着在启动服务时未捕获到异常.看看本地计算机上的Windows服务已启动,然后停止错误,以获取寻找该异常的提示.

根据您的描述,我猜问题是由Startup类存在于另一个项目中引起的,您是否尝试过将启动类包含在窗口服务项目中?

此外,来自HStackOverflow的链接( https://github.com/danesparza/OWIN- WebAPI-Service ),展示了一种变通方法,可从不同项目中加载控制器,或将程序集动态解析为当前的AppDomain.我想这也是值得尝试的.

希望这会有所帮助.

I'm trying to run a Web API application as a Windows Service using OWIN. However, I get the following message, when trying to start the service:

The [ServiceName] service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.

For some reason my service doesn't understand that it should keep listening to http://localhost:9000

The VS solution consists of two projects: The Web API and the Windows service.

In the Windows service project I have:

static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}

public partial class Service : ServiceBase
{
    private const string _baseAddress = "http://localhost:9000/";
    private IDisposable _server = null;

    public Service()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        _server = WebApp.Start<Startup>(url: _baseAddress);
    }

    protected override void OnStop()
    {
        if (_server != null)
        {
            _server.Dispose();
        }
        base.OnStop();
    }
}

In OnStart the Startup refers to the Startup class in the Web API project:

public class Startup
{
    public void Configuration(IAppBuilder builder)
    {
        var config = new HttpConfiguration();        
        config.Routes.MapHttpRoute("Default",
            "{controller}/{id}",
            new { id = RouteParameter.Optional });
        builder.UseWebApi(config);
    }
}

(It also contains some configurations for Unity and logging.)

I guess the installer part of the Windows service project is mostly irrelevant, though it could be useful to know that I have:

this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;

What I've tried:

  • To host the Web API using a console application and then host the console application as a Windows Service. But even though I included 'Console.ReadKey()', it simply stopped.
  • To follow this guide: OWIN-WebAPI-Service The weird thing is that I can get his service to work, but when I tried changing my code to match his set-up, I kept getting the same error.

Full source code: github.com/SabrinaMH/RoundTheClock-Backend/tree/exploring_hosting

解决方案

When you are getting 'service on Local Computer started and then stopped', generally means there's uncaught exception while starting the service. Take a look at Windows service on Local Computer started and then stopped error, for tips to look for that exception.

Based on what you described, my guess the issue is caused by the Startup class exists on a different project, have you tried to have the startup class within the window service project?

Also, the link from HStackOverflow (https://github.com/danesparza/OWIN-WebAPI-Service), shows a work-around approach to load controllers from different project, or dynamically resolve assembly into the current AppDomain. I guess that's also worth trying.

Hope this helps.

这篇关于使用OWIN将Web API托管为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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