9

我有一个 WCF 服务托管在我设置为自动的 Windows 服务中,因此它会在服务器启动时自动启动。该服务的端点由 MSMQ 支持。

当我手动启动服务时,一切都很好。但是当服务在启动时启动时,我得到一个 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 还没有准备好使用……有解决方案吗?

4

1 回答 1

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 支持:如何延迟加载特定服务”中所述。

于 2009-12-18T18:39:13.480 回答