c# - 如何解决 .net Framework 4.7 中无法加载文件或程序集“Microsoft.Extensions.Hosting.Abstractions”问题?

标签 c# azure azure-devops .net-framework-version .net-4.8

当我的 webjob 开始在 azure 门户中准备就绪时,我收到此错误。我检查了 dll,发现只有 Microsoft.Extensions.Hosting 使用“Microsoft.Extensions.Hosting.Abstractions”v 2.1.0

我该如何解决这个问题?我的项目是.net Framework 4.7(请不要忘记这个细节)

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Hosting.Abstractions, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

我的 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
    <IsWebJobProject>true</IsWebJobProject>
    <WebJobName>$(AssemblyName)</WebJobName>
    <WebJobType>Continuous</WebJobType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.30" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.9" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
    <PackageReference Include="Microsoft.WindowsAzure.ConfigurationManager" Version="3.2.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="3.0.31" />
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.31" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="antlr.runtime">
      <HintPath>..\..\thirdparty\antlr-2.7.4\bin\antlr.runtime.dll</HintPath>
    </Reference>
    <Reference Include="Org.Mentalis.Security">
      <HintPath>..\..\thirdparty\seclib-1.0\bin\Org.Mentalis.Security.dll</HintPath>
    </Reference>
    <Reference Include="Sonic.Client">
      <HintPath>..\..\thirdparty\sonicmq-8.5.1\bin\Sonic.Client.dll</HintPath>
    </Reference>
    <Reference Include="Sonic.Client.Jms.Impl">
      <HintPath>..\..\thirdparty\sonicmq-8.5.1\bin\Sonic.Client.Jms.Impl.dll</HintPath>
    </Reference>
    <Reference Include="Sonic.Jms">
      <HintPath>..\..\thirdparty\sonicmq-8.5.1\bin\Sonic.Jms.dll</HintPath>
    </Reference>
    <Reference Include="Sonic.Jms.Cf.Impl">
      <HintPath>..\..\thirdparty\sonicmq-8.5.1\bin\Sonic.Jms.Cf.Impl.dll</HintPath>
    </Reference>
    <Reference Include="Sonic.Security">
      <HintPath>..\..\thirdparty\sonicmq-8.5.1\bin\Sonic.Security.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Settings.job">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

我更新了我的 csproj 文件,添加了:

<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.0" /> <!-- Add this line -->

但是我收到了这个错误:

##[错误]nuget 命令失败,退出代码 (1) 和错误(NU1605:检测到包降级:Microsoft.Extensions.Hosting.Abstractions 从 2.1.1 到 2.1.0。直接从项目引用包选择不同的版本。 让我更新当我将 Microsoft.Extensions.Configuration.Abstractions 从 6.0.0 降级到 2.1.1 时。

enter image description here

最佳答案

我已经安装了软件包Microsoft.Azure.WebJobs.Logging.ApplicationInsights版本3.0.30 .

您可以看到包含以下版本。

enter image description here

  • 再次,正如您的 .csproj 中提到的那样,我已经安装了Microsoft.Azure.WebJobs版本3.0.31 .

  • Microsoft.Azure.WebJobs已经包含在内,我们再次引用包的新版本。

enter image description here

旧版本将被新版本以及与Microsoft.Extensions.Hosting.Abstractions相关的以下设置覆盖。添加于 web.config文件。

 <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Hosting.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.1.0" newVersion="2.1.1.0" />
 </dependentAssembly>     
  • 并且在 .csproj还包括以下设置。
  <Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
   <HintPath>packages\Microsoft.Extensions.Configuration.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
 </Reference>
  <Content Include="packages\Microsoft.Extensions.Hosting.Abstractions.2.1.1\.signature.p7s" />
  <Content Include="packages\Microsoft.Extensions.Hosting.Abstractions.2.1.1\Microsoft.Extensions.Hosting.Abstractions.2.1.1.nupkg" />
  • 您的.csproj错过了这些引用资料。

请确保将上述标签添加到 Web.config 中和.csproj文件以避免版本冲突。

System.IO.FileLoadException: Could not load file or assembly Microsoft.Extensions.Hosting.Abstractions, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

感谢@Cleptus 的评论。

从评论中我了解到您已直接在 .csproj 中更新了软件包文件。

是的,按照错误消息中的建议,而不是直接在 .csproj 中更新,从 NuGet 包更新包版本。

完全删除现有包,构建应用程序并再次安装所需的包。

关于c# - 如何解决 .net Framework 4.7 中无法加载文件或程序集“Microsoft.Extensions.Hosting.Abstractions”问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76347406/

相关文章:

c# - 单元测试 EntityFrameworkCore IEntityTypeConfiguration<T>

java - Junit 测试中 SpringCloud Azure 适配器出现 NullPointerException

Azure DevOps : ##[error]Error: No package found with specified pattern: d:\a\r1\a\**\*. zip

jasmine - Protractor Jasmine Reporter + BDD + Azure DevOps

powershell - VSTS 任务组 Powershell 参数

c# - 检查状态生存时间是否未过期

c# - 泛型和 "One of the parameters of a binary operator must be the containing type"错误

azure - 在仪表板上显示正在运行的 Azure VM 的数量

sql-server - Microsoft Azure SQL 数据库跟踪重播

c# - 遍历 0's and 1' 的 int[,] 数组