Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly Microsoft.Extensions.Logging.Abstractions

I'm using VS2019 Pro v16.3.5

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: 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.

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" />

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.Application\Interfaces.Avaloq.Application.csproj" />
    <ProjectReference Include="..\Interfaces.Avaloq.Common\Interfaces.Avaloq.Common.csproj" />
    <ProjectReference Include="..\Interfaces.Avaloq.Infrastructure\Interfaces.Avaloq.Infrastructure.csproj" />
    <ProjectReference Include="..\Interfaces.Avaloq.Persistence\Interfaces.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>

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:\Users\bowman_rob_a.nuget\packages". 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.

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

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

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 Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.abstractions\2.2.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 Files\dotnet\shared." 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 Files\dotnet\shared\Microsoft.AspNetCore.App\2.2.4\Microsoft.Extensions.Logging.Abstractions.dll" does contain v2.2.0.0 of the dll.

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!

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.

like image 664
Rob Bowman Avatar asked Feb 03 '23 17:02

Rob Bowman


2 Answers

Make sure none of your packages is above the .NET version of the project.

In my case the installed version of Microsoft.Extensions.Http was 5.0 while the project was in .NET Core 3.1. As soon as I downgraded the library to 3.1 everything went smoothly.

Source: https://github.com/Azure/azure-functions-core-tools/issues/2304

like image 156
Luis Gouveia Avatar answered Feb 06 '23 07:02

Luis Gouveia


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

It is listed with a strange path "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.abstractions\2.2.0"

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

They differ from package sources in that the package assets will be referenced directly and will not be copied into the user's packages folder.

That's why you can't find the package in Global Packages like C:\Users\xxx\.nuget\packages.

For Version Conflict

As you mentioned above, the chain of Microsoft.Azure.WebJobs.Extensions is:

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

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.

In addition:

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" />

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

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.

like image 31
LoLance Avatar answered Feb 06 '23 09:02

LoLance