在Windows service或IIS中托管WCF服务时,出现0x80040154异常

本文关键字:服务 出现 异常 0x80040154 WCF service Windows IIS | 更新日期: 2023-09-27 18:09:12

我有一个WCF服务(c#),我可以在Visual Studio 2010中构建和运行(通过Cassini Development Server)。我将控制台客户端应用程序连接到该服务,一切都运行良好。服务本身加载一个COM对象,我的所有代码都按预期工作(到目前为止)。

然而,如果我在Windows服务中托管此服务并使用sc或installutil(本地或远程)安装该服务,然后将控制台客户端应用程序连接到相同的服务,它仍然可以连接和断开连接,但是当我调用加载COM对象的服务方法时,我得到一个异常。每当我在Windows服务或IIS7 64位中托管服务时,我都目睹了这种例外。

很明显,COM对象已经在我的机器上被regsvr32d了,当通过调试器托管时,服务可以加载COM对象。我甚至跳过了COM加载位,对象看起来很棒,我使用它。我们也有其他应用程序使用相同的COM对象。

我认为这(也许)与它是一个(x86) .dll的事实有关,我的开发环境是64位的,并且由于某种原因卡西尼开发服务器可以加载COM对象,但是当我将服务作为Windows服务或在IIS中托管时,无论是$path$变量还是一些环境问题都不允许它找到/加载COM对象。

至于在IIS中托管时,我确实创建了一个新池并启用了32位应用程序,但我仍然在加载COM对象时遇到了问题。至于Windows服务,我不确定我能做些什么……

如果我的谷歌搜索没有效果,请原谅我,但我一直很难找到确切的情况。

下面是标准的未注册COM异常:

System.ServiceModel.FaultException'`1 was unhandled
Message=Retrieving the COM class factory for component with CLSID {BB9F026E-EC18-449F-84AA-677225967C07} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Source=mscorlib
StackTrace:
Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at methodname(String product, String options, Int32 quantity)
   at methodname(String product, String options, Int32 quantity) in ...'Reference.cs:line 70
   at ....Program.Main(String[] args) in ...'Program.cs:line 72
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException:

在Windows service或IIS中托管WCF服务时,出现0x80040154异常

我认为问题是我没有用特殊的64位程序(仍然称为regsvr32)注册.dll,在Windows的SysWOW64目录内。

看:注册32位COM DLL到64位Windows 7

实际上我发现这个解决方案之前,我张贴,并认为我已经尝试过了,仍然得到例外,但也许我错了。这个解决方案似乎奏效了。它甚至可能需要重新启动服务器或至少重新启动IIS。