错误 System.BadImageFormatException 服务织物。

7

问题

我正在构建 Service Fabric 应用程序。当我创建一个项目并运行它时,它可以正常工作。但是当我在 API 控制器中注入一个服务时,它会给我返回以下错误。我已经尝试过解决这个问题,但还没有成功。

错误

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

图片

enter image description here

我添加了服务。

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();
                    }))
            };
        }

2
请确保所有依赖项都是64位的,因为Service Fabric仅支持64位应用程序。 - Peter Bons
2
可能是 Service Fabric System.BadImageFormatException 的重复问题。 - Peter Bons
@PeterBons 所有项目都是 x64,至于确保 NuGet 包是 x64,我不知道该怎么做。 - Alex Gordon
4个回答

4
很可能您的服务或其任何依赖项正在针对x86平台。为了解决这个问题,您需要强制使您的服务在x64上运行和/或替换任何x86依赖项为x64。如果您正在运行dotnet-core,请确保也安装了x64工具。您还可以尝试从项目中删除对Microsoft.VisualStudio.Web.CodeGeneration.Design的引用,如此处所述。这些SO问题可能会给您更多信息:service fabric system badimageformatexception和badimageformatexception when migrating from aspnet core 1.1 to 2.0。

如何确保dotnet core工具是x64? - Alex Gordon
可能最简单的检查方法是验证环境变量中的路径。打开命令提示符(cmd)并输入path,它将列出已安装软件的路径,其中dotnet将是其中之一。如果是x86,则安装在"c:\program files (x86)\dotnet"上。如果是x64,则安装在"c:\program files\dotnet"上。如果两者都有,请转到环境变量并重新排序,以使x64排在第一位。 环境变量位于:右键单击“This Pc”>属性>高级系统设置>环境变量>系统变量。 - Diego Mendes
都没有安装 - Alex Gordon

3
这是我们之前遇到的一个问题,当您尝试添加asp.net核心控制器时会出现。由于某些原因,Visual Studio会添加对“Microsoft.VisualStudio.Web.CodeGeneration.Design”的引用。
我的建议是删除该引用,也不要使用项目->添加->控制器来添加控制器。只需添加一个基本代码文件,并从早期的控制器中复制内容即可。

enter image description here


1

1
Service Fabric 项目必须是 x64。 - Alex Gordon
那么,如果您不确定您的答案有多少价值,我就不确定了。 - Alex Gordon

1

为该软件包添加更多二进制文件,并将平台目标设置为 AnyCPU。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接