【发布时间】:2012-06-06 09:06:58
【问题描述】:
我正在尝试编写简单的 wcf 服务自托管在 Windows 服务中 但是windows服务的ServiceHandle总是0
我需要使用 RegisterDeviceNotification 检测硬件变化 它的参数之一是Handle,在我的例子中是ServiceHandle
public partial class MyService : ServiceBase, IMyService
{
private ServiceHost host;
public static void Main()
{
ServiceBase.Run(new MyService());
}
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
host = new ServiceHost(typeof(MyService), new Uri(@"net.pipe://localhost/MyService"));
host.Open();
}
catch (Exception e)
{
EventLog.WriteEntry("MyService:", e.Message);
}
}
protected override void OnStop()
{
host.Close();
}
#region IMyService Members
public void Register()
{
//Here the ServiceHost is 0
}
#endregion
}
什么会导致问题? 谢谢
【问题讨论】:
-
你需要ServiceHandle做什么?
-
哇哦!服务类不应该执行操作契约!!将其分开,并让一个新类实现服务契约。
标签: c# wcf windows-services