我无法编译x86格式的标准"WCF服务库"

Con*_*ngo 9 wcf

如果我启动Microsoft Visual Studio,创建一个标准的"WCF服务库",并编译它,它完美地工作.

但是,如果我进入Properties..Build并将"Platform Target"设置为"x86",它将无法编译.我需要"x86",因为我需要引用一些.dll,如果选择"所有平台",它将无法工作.

-----start error-----
System.BadImageFormatException: Could not load file or assembly 'file:///D:\backtest\C#\WCF service\WcfServiceLibrary\bin\Debug\WcfServiceLibrary.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'file:///D:\backtest\C#\WCF service for\WcfServiceLibrary\bin\Debug\WcfServiceLibrary.dll'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)

=== Pre-bind state information ===
LOG: User = dr-satellite\Shane
LOG: Where-ref bind. Location = D:\backtest\C#\WCF service for\WcfServiceLibrary\bin\Debug\WcfServiceLibrary.dll
LOG: Appbase = file:///D:/backtest/C#/WCF service for/WcfServiceLibrary/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:\backtest\C#\WCF service for\WcfServiceLibrary\bin\Debug\WcfServiceLibrary.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:/backtest/C#/WCF service for/WcfServiceLibrary/bin/Debug/WcfServiceLibrary.dll.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
-----end error-----
Run Code Online (Sandbox Code Playgroud)

Str*_*tch 13

您的问题实际上是WCF测试客户端抱怨您的WCF服务库是32位.这是因为默认情况下,WCF测试客户端始终以64位模式运行.

我没有花时间更改为WCF服务应用程序以便进行调试,而是在此博客上找到了以下步骤,使WCF测试客户端以32位模式运行 - 因此您不会看到已发布的错误.

  1. 将WcfSvcHost.exe和WcfTestClient.exe从C:\ program files(x86)\ Microsoft Visual Studio 10.0\Common7\IDE复制到本地目录.当然,请保留此文件的备份副本.
  2. 启动Visual Studio 2010命令提示符(开始菜单中的一个链接 - > Visaul Studio 2010)
  3. "cd"到您的WcfSvcHost副本所在的目录.

  4. 执行命令"corflags/32BIT +/FORCE WcfSvcHost.exe"

  5. 执行命令"corflags/32BIT +/FORCE WcfTestClient"

  6. 将文件复制回您找到的位置.

现在您的WcfSvcHost和WcfTestClient将以32位模式运行,您可以调试!

  • 我知道这是旧的"ish"帖子,但请注意,这不适用于2012 VS11,因为两个.exe都已签名,并且您最终收到警告,并且它不允许您更改此内容! (3认同)

Con*_*ngo 7

我找到了两种解决这个问题的好方法:

  1. 安装64位版本的Matlab意味着您可以编译与64位WCF服务库兼容的64位.dll.这消除了在32位模式下编译的需要,因此在调用32位程序集时不会抛出异常.此解决方案效果很好.
  2. 您可以在MSVS 2010中生成两种类型的项目:"WCF服务库"和"WCF服务应用程序".虽然您无法在32位模式下构建和运行"WCF服务库"(它只支持64位),但您可以在32位模式下构建和运行"WCF服务应用程序".

  • 你的意思是"WCF服务_Library_"(因为它不会在32位模式下构建),...."??? (2认同)