1

好吧,我正在構建一個客戶端服務器應用程序,它使用WCF和由WinForms應用程序託管和使用的NetTcpBinding。我在Windows 7 Ultimate x64中使用VS.2012 Ultimate。WcfSvcHost當執行WCF服務應用程序時出現BadImageFormatException

我可以啓動承載服務的winform,並啓動服務。現在,在客戶端,我開始在客戶端應用程序,並立即得到了微軟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參考其定義服務(接口&實現類)。那麼,爲什麼服務器不會抱怨呢?

此外,我已確保此解決方案中的所有項目都將其調試&發行版配置設置爲相同的.NET Framework(4.5)和SAME目標平臺(x86而不是任何CPU)。

此外,對於相同的框架(4.5)和相同的平臺(x86),唯一的外部程序集引用(實用程序庫DLL)也是使用相同的VS版本構建的。

那麼這個不好的圖像格式來自哪裏呢?該解決方案擁有自己的外部庫副本(以確保它具有合適的平臺&框架配置),並且項目文件已被修改,以便它採用外部程序集的相應版本/調試版本。

繼從我使用註冊表編輯器在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. 

這對我並沒有告訴我用什麼...

+0

關於它的任何最終解決方案? – Kiquenet

回答

0

可能是在問題:

日誌:從C使用計算機配置文件:\ WINDOWS \ Microsoft.NET \框架 \ v 4.0 .30319 \ config \ machine.config。

看來應用程序在.NET 4.0 x64下啓動。 嘗試分析您的程序集(例如,通過.NET Framework SDK中的corflags.exe)以確定所需的平臺和.NET版本。

+0

什麼是64位框架的問題? –

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)的\微軟的Visual Studio 10.0 \ Common7 \ IDE

複製WcfSvcHost.exe WcfSvcHost32.exe 1文件(S)複製。

C:\ Program Files文件(x86)的\微軟的Visual Studio 10.0 \ Common7 \ IDE

corflags位/ 32位+ /力WcfSvcHost32.exe彌crosoft(R).NET Framework CorFlags轉換工具。版本4.0.30319.1版權所有(c) 微軟公司。版權所有。

corflags:warning CF011:指定的文件是強名稱簽名。
使用/強制將使該圖像的簽名無效,並且 將要求放棄該程序集。

C:\ Program Files文件(x86)的\微軟的Visual Studio 10.0 \ Common7 \ IDE

然後你可以舉辦各種命令你的項目:

WcfSvcHost32.exe /服務:% enteryourservicenamehere%.dll /config:%yourconfigfilehere.dll.config%

這就是爲我工作的。

相關問題