【问题标题】:Error System.BadImageFormatException service fabric错误 System.BadImageFormatException 服务结构
【发布时间】:2018-07-23 11:32:59
【问题描述】:

问题

我正在构建服务结构应用程序。当我创建一个项目并运行它时,它工作正常。但是当我在 Api 控制器中注入服务时,它给了我这个错误,我试图解决它但还没有成功。

错误

System.BadImageFormatException 无法加载文件或程序集“dotnet-aspnet-codegenerator-design”或其依赖项之一。试图加载格式不正确的程序。

图片

我添加服务

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            return new ServiceInstanceListener[]
            {
                new ServiceInstanceListener(serviceContext =>
                    new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                        return new WebHostBuilder()
                                    .UseKestrel()
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatelessServiceContext>(serviceContext)
                                            .AddScoped(typeof(ISendMessage), typeof(SendMessage))
                                            )
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                    .UseUrls(url)
                                    .Build();
                    }))
            };
        }

【问题讨论】:

  • 确保所有依赖项都是 64 位的,因为 Service Fabric 仅支持 64 位应用程序。
  • @PeterBons 所有项目都是 x64,至于确保 nuget 包是 x64,我不知道该怎么做

标签: azure asp.net-core azure-service-fabric service-fabric-stateless


【解决方案1】:

这是我们前段时间遇到的一个问题,当您尝试添加 asp.net 核心控制器时会出现此问题。出于某种原因,Visual Studio 添加了对“Microsoft.VisualStudio.Web.CodeGeneration.Design”的引用。

我的建议是删除引用,也不使用 Project->Add->Controller 添加控制器。

只需添加一个基本代码文件并从早期控制器复制内容即可。

【讨论】:

    【解决方案2】:

    为包添加更多二进制文件,并为平台目标执行 AnyCPU。

    <PackageId>Microsoft.VisualStudio.Web.CodeGeneration.Design</PackageId>
     <RuntimeIdentifier Condition=" '$(TargetFramework)' != 'netcoreapp2.0' ">win7-x86</RuntimeIdentifier>
     <RuntimeIdentifier Condition=" '$(TargetFramework)' != 'netcoreapp2.0' ">win7-x64</RuntimeIdentifier>
    

    【讨论】:

      【解决方案3】:

      您的服务或其任何依赖项很可能针对的是 x86 平台

      要解决这个问题,您必须强制您的服务在 x64 上运行和/或替换 x64 的任何 x86 依赖项。

      如果您正在运行 dotnet-core,请确保也安装了 x64 工具。

      您也可以尝试从您的项目中删除对Microsoft.VisualStudio.Web.CodeGeneration.Design 的引用,如here 所述

      这些 SO 问题可能会为您提供更多信息:

      service fabric system badimageformatexception

      badimageformatexception when migrating from aspnet core 1.1 to 2.0

      【讨论】:

      • 我如何确保 dotnet 核心工具是 x64 的?
      • 可能最简单的检查方法是验证环境变量的路径。转到您的命令提示符(cmd)并输入路径,它将列出已安装软件的路径,dotnet 将是其中之一。如果是 x86,它将安装在“c:\program files (x86)\dotnet”上。如果是 x64,它将安装在“c:\program files\dotnet”上。如果两者都有,请转到环境变量并将它们重新排序以首先具有 x64。环境变量位于:右键单击“这台电脑”>属性>高级系统设置>环境变量>系统变量。
      • 两个都没有安装
      【解决方案4】:

      要修复它,只需按照以下步骤操作:

      • 打开项目属性窗口。
      • 选择构建选项卡
      • 将其更改为“任何 CPU”
      • 保存您的更改。
      • 编译您的项目并运行

      编辑 1:

      Service febric targets x64 bit 然后单击平台下拉菜单并选择新建,然后创建 x64 平台配置。

      【讨论】:

      • 服务结构项目必须是 x64
      • 那么我不确定你的答案有多大价值
      猜你喜欢
      • 2017-09-21
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多