在XP机器上启动System.Net.HttpListener需要哪些Windows服务?

时间:2013-01-10 08:09:25

标签: .net windows-services dependencies httplistener

在我们的一些客户的WinXP SP3机器上,几乎每天都会发生一种奇怪的行为。

我们安装了一个Windows服务,在启动时执行以下代码:

if( !HttpListener.IsSupported )
{
    throw new Exception( string.Format( "HttpListener is not supported on {0}.", Environment.OSVersion ) );
}

_httpListener = new HttpListener();
_httpListener.Prefixes.Add( "http://localhost:20001/" );

_thread = new Thread( new ThreadStart( StartListening ) );
_thread.Start();

现在,有时当Windows启动时,代码会抛出“不支持”异常。停止并再次启动服务后,HttpListener正常工作!

我的Windows服务需要服务依赖吗?

1 个答案:

答案 0 :(得分:0)

找到了解决方案!

在ServiceInstaller构造函数中,只需添加以下行:

serviceInstaller.ServicesDependedOn = new string[] { "HTTP" };

这使您自己的服务依赖于Windows HTTP服务,并且只在HTTP Servive成功启动时启动。