执行WCF服务应用程序时WcfSvcHost BadImageFormatException

时间:2013-01-12 03:53:30

标签: wcf visual-studio-2012 nettcpbinding servicehost badimageformatexception

好的,我正在使用WCF构建一个客户端服务器应用程序,其中NetTcpBinding由WinForms应用程序托管和使用。我在Windows 7 Ultimate x64中使用VS.2012 Ultimate。

我可以启动托管服务的winform,然后我可以启动该服务。现在,在客户端,我启动客户端应用程序,并立即获得一个Microsoft WCF服务主机窗口告诉我:

System.BadImageFormatException. Could not load file or assembly X.Services.dll ... an
attempt was made to load a program with an incorrect format.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)

现在,客户端和服务器WinForm项目都引用了定义服务的X.Services.dll(接口和实现类)。那么,为什么服务器不会抱怨呢?

另外,我确保此解决方案中的所有项目都具有他们的Debug& amp;发布配置设置为相同的.NET Framework(4.5)和SAME目标平台(x86而不是任何CPU)。

此外,对于相同的框架(4.5)和相同的平台(x86),唯一的外部程序集引用(实用程序库DLL)也使用相同的VS版本构建。

那么这种糟糕的图像格式来自哪里?该解决方案有自己的外部库副本(以确保它具有正确的平台和框架配置),并且项目文件已经过修改,因此它需要相应的外部程序集的发布/调试版本。

根据异常提示,我使用regedit在HKLM中添加一个密钥以启用程序集加载/绑定日志记录并获得这些额外信息:

=== Pre-bind state information ===
LOG: User = DOMAIN\Username
LOG: Where-ref bind. Location =    D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services.dll
LOG: Appbase = file:///D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be      probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///D:\Sources\My.Net\Code\T\X.Services\bin\Debug\X.Services.dll.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

对我来说并没有告诉我任何使用......

2 个答案:

答案 0 :(得分:0)

可能是以下问题:

  

日志:使用C:\ Windows \ Microsoft.NET \ Framework 64 \ v 4.0 中的计算机配置文件 0.30319 \设置\ machine.config中。

似乎应用程序在.NET 4.0 x64下启动。 尝试分析您的程序集(例如,通过.NET Framework SDK中的corflags.exe)来确定所需的平台和.NET版本。

答案 1 :(得分:0)

我不知道这是否仍然有效,但它与this thread中的问题相同。 解决方案是在x86中运行每个进程,如here

最简单的方法是创建第二个WcfSvcHost作为x86变体,如下所示:

  

启动具有管理员权限的cmd环境:

     

C:\

     
    

cd" c:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ IDE"

  
     

c:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ IDE

     
    

复制WcfSvcHost.exe WcfSvcHost32.exe             1个文件被复制。

  
     

c:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ IDE

     
    

corflags / 32BIT + / Force WcfSvcHost32.exe Microsoft(R).NET Framework CorFlags转换工具。版本4.0.30319.1版权所有(c)     微软公司。保留所有权利。

  
     

corflags:警告CF011:指定的文件是强名称签名。
  使用/ Force将使此图像的签名无效   要求组装辞职。

     

c:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ IDE

然后,您可以使用以下命令托管项目:

  

WcfSvcHost32.exe /service:%enteryourservicenamehere%.dll /config:%yourconfigfilehere.dll.config%

这对我有用。