2009-12-18 81 views
9

我有一个WCF服务托管在Windows服务中,我将其设置为自动,以便在服务器启动时自动启动。服务端点是MSMQ支持的。MSMQ支持的Windows服务中托管的WCF服务在启动时失败

当我手动启动服务时,一切都很好。但是,当服务在启动时启动,我得到一个MSMQ例外:

System.TypeInitializationException: The type initializer for 
'System.ServiceModel.Channels.Msmq' threw an exception. ---> 
System.ServiceModel.MsmqException: The version check failed with the error: 
'The Message Queuing service is not available (-1072824309, 0xc00e000b)'. The 
version of MSMQ cannot be detected All operations that are on the queued channel 
will fail. Ensure that MSMQ is installed and is available. 
    at System.ServiceModel.Channels.MsmqQueue.GetMsmqInformation 
        (Version& version, Boolean& activeDirectoryEnabled) 
    at System.ServiceModel.Channels.Msmq..cctor() 
    --- End of inner exception stack trace --- 

这似乎是MSMQ不准备在服务启动之前使用...有一个解决的办法?

回答

7

您需要在WCF服务主机上添加对MSMQ的依赖关系。您可以在安装程序的服务做到这一点:如果你没有使用服务安装

ServiceInstaller serviceInstaller = new ServiceInstaller(); 
// Adding this property to your ServiceInstaller forces 
// your service to start after MSMQ. 
serviceInstaller.ServicesDependedOn = new string[] { "MSMQ" }; 

,您还可以通过编辑Windows注册表中添加MSMQ依赖为您服务,如“Microsoft Support: How to delay loading of specific services”描述。

+0

谢谢!谷歌的异常消息证明是徒劳的! – puffpio