WCF 服务 BadImageFormatException

Posted

技术标签:

【中文标题】WCF 服务 BadImageFormatException【英文标题】:WCF Service BadImageFormatException 【发布时间】:2020-10-08 07:38:25 【问题描述】:

我刚刚创建了一个 wcf 服务库,并且正在使用一个简单的控制台应用程序来连接它。但是,当我启动控制台应用程序时,wcf 服务主机打开并显示此错误:

System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\uop\source\repos\WPF\APPONE\APPONE.Service\bin\x86\Debug\APPONE.Service.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'file:///C:\Users\amosa\source\repos\WPF\APPONE\APPONE.Service\bin\x86\Debug\APPONE.Service.dll'
   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)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

我已确保我的所有应用都运行 x86。

我试图查看它是否是我的程序中的问题,但它甚至没有到达我程序中的第一个调试点:

static void Main(string[] args)

    // Step 1: Create a URI to serve as the base address.
    Uri baseAddress = new Uri("http://localhost:8733/APPONE/");

    // Step 2: Create a ServiceHost instance.
    ServiceHost selfHost = new ServiceHost(typeof(DocumentType), baseAddress);

    try
    
        selfHost.AddServiceEndpoint(typeof(IDocumentType), new WSHttpBinding(), "DocumentType");
        // Step 4: Enable metadata exchange.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        selfHost.Description.Behaviors.Add(smb);

        // Step 5: Start the service.
        selfHost.Open();
        Console.WriteLine("The service is ready.");

        // Close the ServiceHost to stop the service.
        Console.WriteLine("Press <Enter> to terminate the service.");
        Console.WriteLine();
        Console.ReadLine();
        selfHost.Close();

    
    catch(Exception e)
    
        Console.WriteLine("An exception occurred: 0", e.Message);
        selfHost.Abort();
    

【问题讨论】:

【参考方案1】:

根据您的描述,我做了测试。首先,您需要将平台目标值设置为“Any CPU”:

然后将你的项目构建成可执行文件并以管理员身份运行:

另外,我使用的.Net框架是4.7.2。其实如果你用x86在VS中调试会报以上错误,你直接把工程编译成可执行文件,然后运行为管理员,没关系。

【讨论】:

以上是关于WCF 服务 BadImageFormatException的主要内容,如果未能解决你的问题,请参考以下文章

WCF 服务证书认证

WCF / WCF 数据服务 / WCF RIA 服务

WCF 一步一步 发布 WCF服务 到 IIS (图)

WCF 服务库和 WCF 服务应用程序之间的区别

WCF 数据服务还是只是 WCF 服务?

WCF - WCF基本概念和简单WCF服务端创建