使用 OWIN 将 Web API 托管为 Windows 服务
Host Web API as Windows Service using OWIN
我正在尝试使用 OWIN 运行 Web API 应用程序作为 Windows 服务。但是,在尝试启动服务时,我收到以下消息:
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.
出于某种原因,我的服务不理解它应该继续监听 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指的是Startup中的class网络 API 项目:
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和logging的一些配置。)
我想 Windows 服务项目的安装程序部分大部分是无关紧要的,但知道我有:
可能会有用
this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
我试过的:
- 使用控制台应用程序托管 Web API,然后将控制台应用程序作为 Windows 服务托管。但是即使我包含了 'Console.ReadKey()',它也只是停止了。
- 要遵循本指南:
OWIN-WebAPI-Service
奇怪的是我可以让他的服务正常工作,但是当我尝试更改我的代码以匹配他的设置时,我总是遇到同样的错误。
完整源代码:
github.com/SabrinaMH/RoundTheClock-Backend/tree/exploring_hosting
当您得到 'service on Local Computer started and then stopped' 时,通常意味着在启动服务时有未捕获的异常。查看 Windows service on Local Computer started and then stopped error,获取查找该异常的提示。
根据您的描述,我猜问题是由 Startup class 存在于不同的项目中引起的,您是否尝试过在 window 中启动 class服务项目?
此外,来自 HWhosebug (https://github.com/danesparza/OWIN-WebAPI-Service) 的 link 显示了一种变通方法,用于从不同项目加载控制器,或将程序集动态解析到当前 AppDomain 中。我想这也值得一试。
希望对您有所帮助。
对于该示例 OWIN-WebAPI-Service,您必须安装软件包
Install-Package Microsoft.Owin.Host.HttpListener
我正在尝试使用 OWIN 运行 Web API 应用程序作为 Windows 服务。但是,在尝试启动服务时,我收到以下消息:
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.
出于某种原因,我的服务不理解它应该继续监听 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指的是Startup中的class网络 API 项目:
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和logging的一些配置。)
我想 Windows 服务项目的安装程序部分大部分是无关紧要的,但知道我有:
可能会有用this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
我试过的:
- 使用控制台应用程序托管 Web API,然后将控制台应用程序作为 Windows 服务托管。但是即使我包含了 'Console.ReadKey()',它也只是停止了。
- 要遵循本指南: OWIN-WebAPI-Service 奇怪的是我可以让他的服务正常工作,但是当我尝试更改我的代码以匹配他的设置时,我总是遇到同样的错误。
完整源代码: github.com/SabrinaMH/RoundTheClock-Backend/tree/exploring_hosting
当您得到 'service on Local Computer started and then stopped' 时,通常意味着在启动服务时有未捕获的异常。查看 Windows service on Local Computer started and then stopped error,获取查找该异常的提示。
根据您的描述,我猜问题是由 Startup class 存在于不同的项目中引起的,您是否尝试过在 window 中启动 class服务项目?
此外,来自 HWhosebug (https://github.com/danesparza/OWIN-WebAPI-Service) 的 link 显示了一种变通方法,用于从不同项目加载控制器,或将程序集动态解析到当前 AppDomain 中。我想这也值得一试。
希望对您有所帮助。
对于该示例 OWIN-WebAPI-Service,您必须安装软件包
Install-Package Microsoft.Owin.Host.HttpListener