无法加载文件或程序集 Microsoft.Extensions.Logging.Abstractions

问题描述:

我使用的是 VS2019 Pro v16.3.5

I'm using VS2019 Pro v16.3.5

我有一个包含 Azure 函数项目的解决方案,该项目引用了多个类库项目.

I have a solution containing an Azure functions project which references several class library projects.

*项目编译失败,错误如下:

The top-level project fails to compile with the following error:

System.IO.FileNotFoundException: 无法加载文件或程序集'Microsoft.Extensions.Logging.Abstractions,版本=2.2.0.0,文化=中性,PublicKeyToken=adb9793829ddae60'.系统不能找到指定的文件.

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

该项目确实有一个包引用:PackageReference Include="Microsoft.AspNetCore.App" 并且这个框架包含缺少的 dll,所以我不知道为什么会遇到问题.

The project does have a package reference: PackageReference Include="Microsoft.AspNetCore.App" and this framework includes the missing dll, so I don't know why it's having trouble.

我的想法可能是引用的项目之一取决于不同的版本,但我没有看到.

My thoughts are perhaps one of the referenced projects is depending on a different version but I don't see it.

我确实尝试在*项目中明确引用包,但这没有任何区别:

I did try explicitly referencing the package in the top-level project but this made no difference:

<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0.0" />

这是* csproj 文件的当前副本:

Here is a current copy of the top-level csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Base.Core.SharedKernel" Version="1.0.0.23885" />
    <PackageReference Include="FluentValidation" Version="8.4.0" />
    <PackageReference Include="FluentValidation.AspNetCore" Version="8.4.0" />
    <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="1.8.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="3.0.5" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.27" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..Interfaces.Avaloq.ApplicationInterfaces.Avaloq.Application.csproj" />
    <ProjectReference Include="..Interfaces.Avaloq.CommonInterfaces.Avaloq.Common.csproj" />
    <ProjectReference Include="..Interfaces.Avaloq.InfrastructureInterfaces.Avaloq.Infrastructure.csproj" />
    <ProjectReference Include="..Interfaces.Avaloq.PersistenceInterfaces.Avaloq.Persistence.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

引用 Nuget 包的问题我可以看到工作"包引用的程序集(例如 FluentValidation.dll)可以在C:Usersowman_rob_a.nugetpackages"的全局包文件夹中找到.但是,全局packages文件夹不包含Microsoft.Extensions.Logging.Abstractions的v2.2.0.0,它包含很多版本但从2.1.0跳到3.0.0.

Problems Referencing the Nuget Package I can see that assemblies for the "working" package references (such as FluentValidation.dll) can be found in the global packages folder at "C:Usersowman_rob_a.nugetpackages". However, the global packages folder does not contain v2.2.0.0 of Microsoft.Extensions.Logging.Abstractions, it contains lots of versions but skips from 2.1.0 to 3.0.0.

如果我从包管理器控制台运行:install-package Microsoft.Extensions.Logging.Abstractions -Version 2.2.0"然后我收到以下错误:

If I run from the package manager console: "install-package Microsoft.Extensions.Logging.Abstractions -Version 2.2.0" then I get the following error:

WriteObject 和 WriteError 方法不能从外部调用BeginProcessing、ProcessRecord 和EndProcessing 方法,它们只能从内部调用同一个线程

The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread

尽管出现错误,该包还是会出现在解决方案资源管理器的项目包部分.但是,它列出了一个奇怪的路径C:Program FilesdotnetsdkNuGetFallbackFoldermicrosoft.extensions.logging.abstractions2.2.0"

Despite the error, the package does then appear in the project's packages section of solution explorer. However, it is listed with a strange path "C:Program FilesdotnetsdkNuGetFallbackFoldermicrosoft.extensions.logging.abstractions2.2.0"

引用共享包的问题因为 v2.2.0.0 包含在共享包引用Microsoft.AspNetCore.App"中,那么我想程序集应该从那里拉出来?共享包的程序集位于C:Program Filesdotnetshared"中.共享包有很多版本Microsoft.AspNetCore.App"但再次跳过了 2.2.0.0,从 2.1.13 到 2.2.4.但是,文件夹C:Program FilesdotnetsharedMicrosoft.AspNetCore.App2.2.4Microsoft.Extensions.Logging.Abstractions.dll"确实包含 dll 的 v2.2.0.0.

Problems Referencing the Shared Package Because v2.2.0.0 is included in the shared package reference "Microsoft.AspNetCore.App" then I guess the assembly should be pulled from there? The assemblies for shared packages reside in "C:Program Filesdotnetshared." There are lots of versions of the shared package "Microsoft.AspNetCore.App" but again 2.2.0.0 is skipped, from 2.1.13 to 2.2.4. However, the folder "C:Program FilesdotnetsharedMicrosoft.AspNetCore.App2.2.4Microsoft.Extensions.Logging.Abstractions.dll" does contain v2.2.0.0 of the dll.

版本冲突我认为问题的根本原因可能是 Azure Functions 依赖于 nuget 包链:Microsoft.Azure.WebJobs.Extensions --> Microsoft.Azure.WebJobs --> Microsoft.Extensions.Logging.Abstractions.Microsoft.Azure.WebJobs.Extensions 的最新版本是 3.0.2,这导致 Microsoft.Extentions.Logging.Abstractions 的 v2.1.0 - 这比 Microsoft 共享框架中包含的 v2.2.0.0 旧.AspNetCore.App.有谁知道我如何更改编译器使用的共享框架的版本?我在任何地方都找不到 runtimeconfig.json 文件!

Version Conflict I think the root cause of the problem could be that Azure Functions has a dependency on the nuget package chain: Microsoft.Azure.WebJobs.Extensions --> Microsoft.Azure.WebJobs --> Microsoft.Extensions.Logging.Abstractions. The latest version of Microsoft.Azure.WebJobs.Extensions is 3.0.2 and this leads down to v2.1.0 of Microsoft.Extentions.Logging.Abstractions - this is older than the v2.2.0.0 that's included in the Shared Framework Microsoft.AspNetCore.App. Does anyone know how I can change the version of a Shared Framework that's used by the compiler? I can't find a runtimeconfig.json file anywhere!

解决问题我已经能够通过从解决方案中的所有项目中删除共享引用并单独添加每个必需的 nuget 包来构建解决方案 - 使用较旧的 2.1.0 版本.

Work Around I have been able to make the solution build by removing the Shared Reference from all projects within the solution and adding each required nuget package individually - using the older 2.1.0 versions.

如果其他人遇到类似问题,一些信息和解决方法可能有助于解决难题.

Some info and workaround which may help to resolve the puzzle if someone else meets similar issue.

它以奇怪的路径C:ProgramFilesdotnetsdkNuGetFallbackFoldermicrosoft.extensions.logging.abstractions2.2.0"

It is listed with a strange path "C:Program FilesdotnetsdkNuGetFallbackFoldermicrosoft.extensions.logging.abstractions2.2.0"

FallBackFolders 是用来分享的跨用户和机器打包以降低风险空间.

FallBackFolders are something used to share packages across users and machines to reduce risk space.

它们与包源的不同之处在于包资源将被直接引用,不会被复制到用户的包文件夹中.

这就是为什么你在像 C:Usersxxx.nugetpackages 这样的全局包中找不到包的原因.

That's why you can't find the package in Global Packages like C:Usersxxx.nugetpackages.

对于版本冲突

如上所述,Microsoft.Azure.WebJobs.Extensions 的链是:

Microsoft.Azure.WebJobs.Extensions --> Microsoft.Azure.WebJobs --> Microsoft.Extensions.Logging.Abstractions(2.1.0)

并且由于您引用了 Microsoft.AspNetCore.App 包,因此它的链:

And since you reference the Microsoft.AspNetCore.App package, the chain of it:

Microsoft.AspNetCore.App(2.2.0) --> Microsoft.Extensions.Logging.Abstractions(2.2.0)

我认为这可能是版本冲突的原因.您可以删除该包以检查它是否是另一种解决方法.

I think this could be the cause of the version conflict.You can remove that package to check if it's another workaround.

另外:

如果我在 VS16.3.5 中新建一个 Azure Function 项目,我们不需要手动引用这些包:

If I create a new Azure Function project in VS16.3.5, we don't need to reference these packages manually:

    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="1.8.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="3.0.5" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />

由于Microsoft.NET.Sdk.Functions 依赖于这些包,nuget 将帮助我们下载并引用它们.

Since Microsoft.NET.Sdk.Functions depends on these packages, nuget will help us downlaod and reference them.

我尝试了多种 Azure 函数项目,但它们都不需要 Microsoft.AspNetCore.App 包,因此如果您没有使用该包的特定理由,则不需要不需要引用它.

And I tried several kinds of Azure functions project, but none of them need the Microsoft.AspNetCore.App package, so if you don't have specific reason to use that package, you don't need to reference it.