Sab*_*aMH 9 c# windows-services self-hosting asp.net-web-api owin
我正在尝试使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
(它还包含Unity和日志记录的一些配置.)
我想Windows服务项目的安装程序部分基本上是无关紧要的,尽管知道我有以下内容可能很有用:
this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService;
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
完整源代码: github.com/SabrinaMH/RoundTheClock-Backend/tree/exploring_hosting
小智 4
当您收到“本地计算机上的服务启动然后停止”时,通常意味着启动服务时存在未捕获的异常。查看本地计算机上的 Windows 服务启动然后停止错误,了解查找该异常的提示。
根据您的描述,我猜测问题是由启动类存在于不同的项目中引起的,您是否尝试过在窗口服务项目中使用启动类?
此外,来自 HStackOverflow ( https://github.com/danesparza/OWIN-WebAPI-Service )的链接显示了从不同项目加载控制器或将程序集动态解析到当前 AppDomain 的解决方法。我想这也值得尝试。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
9401 次 |
| 最近记录: |