gpt4 book ai didi

c# - 初始化安装时发生异常 : System. BadImageFormatException:无法加载文件或程序集

转载 作者:行者123 更新时间:2023-12-02 01:54:24 28 4
gpt4 key购买 nike

我有 Worker Services 作为 Windows 服务,我正在尝试将其安装在计算机上,但是当我尝试安装它时,它会抛出以下错误

Microsoft (R) .NET Framework Installation utility Version 4.8.3752.0
Copyright (C) Microsoft Corporation. All rights reserved.

Exception occurred while initializing the installation:
System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\JNyingi\source\repos\SeSPWS\SeSPWS\bin\Release\netcoreapp3.0\SeSPWS.exe' or one of its dependencies. The module was expected to contain an assembly manifest..

下面是正在使用的包的.csproj文件内容

<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>dotnet-SeSPWS-00EEA7BA-8CD9-4E72-B073-FB0FB7B9192A</UserSecretsId>
<ApplicationIcon />
<OutputType>WinExe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.1" />
<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
</ItemGroup>

<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>

在查看程序集绑定(bind)日志查看器时,我看到了此错误日志。

** Assembly Binder Log Entry  (2/14/2020 @ 1:41:17 PM) ***

The operation failed.
Bind result: hr = 0x80131018. No description available.

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\Users\JNyingi\source\repos\SeSPWS\SeSPWS\bin\Release\netcoreapp3.0\SeSPWS.exe
LOG: Appbase = file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = InstallUtil.exe
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: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe.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:///C:/Users/JNyingi/source/repos/SeSPWS/SeSPWS/bin/Release/netcoreapp3.0/SeSPWS.exe.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\JNyingi\source\repos\SeSPWS\SeSPWS\bin\Release\netcoreapp3.0\SeSPWS.exe
LOG: Entering run-from-source setup phase.
ERR: Error extracting manifest import from file (hr = 0x80131018).
ERR: Run-from-source setup phase failed with hr = 0x80131018.
ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.

我已经安装了以下运行时; .NET Core 3.0、ASP.NET Core 3.0 和 Windows 桌面 3.0。但是,即使升级到 .NET Core 3.1 运行时,该服务仍然无法安装。

下面是我的构建配置文件

Build Setup

我已经构建了一个类似的工作服务,之前安装时没有任何麻烦;我希望解决此错误并将服务部署到计算机。

编辑以下是我的程序类

public class Program
{
public static void Main(string[] args)
{
var isService = !(Debugger.IsAttached || args.Contains("--console"));
if (isService)
{
CreateWebHostBuilder(args).Build().RunAsService();
}
else
{
CreateWebHostBuilder(args).Build().Run();
}
}


public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName))
.UseStartup<Startup>()
.UseKestrel((context, serverOptions) =>
{
serverOptions.ListenAnyIP(1042);
serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(1.5);
});
}

编辑2 nUGET packages这些是该服务使用的 NuGet 包

最佳答案

大多数情况下的错误是由于 Application不兼容位数引起的| Platform | Installutil|IIS

我已经汇总了您可以执行的各种检查:

  • 主项目的设置 Project > Properties > Uncheck Prefer 32 bit
  • 主项目的设置 Project > Properties > /platform:AnyCpu或您的目标 BITness (x86) for 32bit
  • 如果您在项目中引用其他 DLL,请确保不存在位不匹配
  • 验证您的App.Config文件并查看是否有行指定不同的目标运行时与应用程序的目标运行时
  • 验证 IIS > AppPool 设置的位数 > Enable 32 bit applications
  • 取消选中 "Use the 64 bit version of IIS Express for Web Sites and Projects" Projects and Solutions下的选项
  • 检查 InstallUtil 的版本您用来安装服务的文件
  • 检查StartUp您的项目,看看它是否是预期的项目
  • 尝试以管理员身份运行InstallUtil(从另一篇文章中可以看出)
  • 从 VS 中取消选中 64 位版本的 IIS Navigate to Visual Studio – Tools – Options – Projects and Solutions – Web Projects
  • 通过查看 .CSProj 验证位数文件
  • 您可以通过使用 FusionLogging 识别导致此异常的程序集来进一步缩小范围。 DependencyWalker工具,您将能够判断哪个 dll 在运行时加载失败

进一步阅读:

这个Medium帖子解释了在.NetCore中创建Windows服务的过程

<小时/>


    编辑: 根据下面 OP 的帖子分解根本原因

  • WebHostBuilder 主要允许我们配置和构建 WebHost,最适合 Web 应用程序。但在 .net core 2.1 中引入了一个新选项“通用”Host,使开发人员能够创建可以运行后台任务、创建服务和处理消息的控制台应用程序。请参阅“Steve Gordons”文章 Generic Host for Windows Services

  • 当我们创建worker服务模板时,我们需要引用Microsoft.Extensions.Hosting.WindowsServices来自努盖特现在我们可以使用 IHostBuilder.UseWindowsService()
    有两种使用方法(请参阅下一点的详细信息):后台工作服务(使用 Microsoft.Net.Sdk.Worker )、Web 应用程序( Microsoft.NET.Sdk.Web )服务示例,区别在于前一种方法中明确引用了 Microsoft.Extensions.Hosting 包 MSDN解释这一点的文章

    • <Project Sdk="Microsoft.NET.Sdk.Web">现在在这里你可以使用IWebHostBuilder但请务必将其用于使用 Razor Pages 或 MVC 框架的基于 Web 应用程序的服务。 A Good example

    • <Project Sdk="Microsoft.NET.Sdk.Worker">这种类型适合大多数Windows服务创建的需求,其中服务需要执行后台任务

  • <RuntimeIdentifier>元素与自包含部署一起使用,并由 .NET 包用来表示 NuGet 包中特定于平台的 Assets

关于c# - 初始化安装时发生异常 : System. BadImageFormatException:无法加载文件或程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60236508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com