Rob Bowman
Rob Bowman

Reputation: 8751

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.

Upvotes: 48

Views: 113326

Answers (14)

System.Cats.Lol
System.Cats.Lol

Reputation: 1780

I was getting this error with a .NET 8 Azure Functions project that uses the in-process model.

To fix it, I added "FUNCTIONS_INPROC_NET8_ENABLED": 1 to the Values section of my local.settings.json file.

Reference

Upvotes: 0

This is something that we stumbled upon very recently on a ASP.NET Core project with VS2022.

Background Information
To give a clear picture about the situation, here are some background information regarding the architecture of the project.

  1. ASP.NET Core API on .NET 6.0 which depends on several libraries from another project (this another project will be called core throughout this post.
  2. core project on .NET 4.7.2.

For many reasons we had to re-use the core library and functionality, in order to avoid a big re-write. Essentially, a lot of this internal functionality had to be exposed through the ASP.NET API.

When the issue about Microsoft.Extensions.Logging.Abstractions started occurring

In order to have a clean release/build pipeline, we modified the core project to output the DLLs in R:\CommonFolder, but the moment the same happened for the ASP.NET API the problem that is discussed here, on this thread popped up.

How to resolve the issue
Resolving this issue is as simple as changing the build configuration to output the obj folder in the same directory as the build folder - in this case R:\CommonFolder.

This is because after moving the main build artifacts (dlls) into another directory, but not the unlinked source binaries (i.e., obj folder) VS seems to be caching several things into the old directory, including information about dependent libraries and NuGet packages - so if you were to had cleared the solution earlier, built in the new R:\CommonFolder but not cleared the relevant source binaries and point them to the new directory, you'd be experiencing the problem discussed here (and many more for that matter).

Upvotes: 0

mitiko
mitiko

Reputation: 826

I've also encountered the same issue when depending on a project that was using ILogger but resolving it from Microsoft.Extensions.Logging instead of Microsoft.NET.Sdk.Functions that the main project uses.

The fix is just:

- <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
+ <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />

Upvotes: 0

zackpep
zackpep

Reputation: 116

I got the same error after updating my Azure Functions project to .NET 8. It was still loading .NET 6 packages. Turn out that when I was doing this (January 2024), .NET 8 did not support the in-process model for Functions.

I reverted the project back to .NET 6 until .NET 8 supports in-process

Another option would be to migrate your project to the isolated model.

Upvotes: 1

Steve Smith
Steve Smith

Reputation: 2270

In my case, VS2022 had added some extra into the project.csproj file which were trying to reference an older version of a Nuget package of which a later version was already being used. Once these were remove, the project compiled properly.

Upvotes: 0

Khalil
Khalil

Reputation: 1107

In case of Azure Function V4. Downgrading to Microsoft.* 6 version libraries solved the issue.

Support for those dependencies is restricted to 6.0.0. This is a limitation of the in-proc model in V4 running on .NET 6.

The recommendation, if this is a requirement, is to move to the Isolated Model, where .NET dependencies are not restricted/unified.

Upvotes: 4

Jonny
Jonny

Reputation: 1171

I had this problem with v4 of Azure Functions. What solved it for me was editing the .csproj-file and adding <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
to <PropertyGroup>.

Upvotes: 8

Gratzy
Gratzy

Reputation: 2908

I just created a new project using .net 6 and ran into this issue.

I installed entity framework nuget packages:

Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools

To support data access into a SQL db.

Both of those libraries defaulted to .net 7 for latest version.

These were the errors I was getting:

[2023-01-16T18:15:16.759Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2023-01-16T18:15:16.790Z] The 'Function1' function is in error: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Even changing the project to .net 7 didn't resolve it, although I wonder if I would have had to update all the nuget packages to 7. I had to downgrade both of those libraries to .net 6 and it compiled and ran fine.

Regardless, I want to remain on 6 with long term support. The only nuisance now is that nuget wants to upgrade those two libraries to .net 7 so they show as available for an upgrade.

Upvotes: 5

Sergio Solorzano
Sergio Solorzano

Reputation: 665

I'm on VS2022 trying to run an httptrigger function of funcapp. I downgraded Microsoft.Extensions.Http from 7.0.0 to 6.0.0 and Microsoft.Extensions.Logging.Abstractions to 6.0.3. This worked fine.

Upvotes: 10

Venu
Venu

Reputation: 11

I had a similar issue on running windows service and had to update config file with newly available version by using redirect technique resolved the issue.

<configuration>
<appSettings />
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.Extensions.Logging" publicKeyToken="adb9793829ddae60" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
</configuration>

Upvotes: 1

IAmCoder
IAmCoder

Reputation: 3442

I encountered this issue after upgrading a Nuget package and then reverting. Had to manually delete all bin and obj folders in the solution before it worked again. Cleaning the solution did not help.

Upvotes: 3

Luis Gouveia
Luis Gouveia

Reputation: 8955

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

Upvotes: 36

sapfile
sapfile

Reputation: 27

I had the same problem in the WebJob project that was compiled in Azure DevOps and running with Azure WebApp.

I have managed to solve the problem by changing "Use .Net Core" task version to the last version (3.1.x) in Build pipeline. enter image description here

This was the error message : Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=.......'. The located assembly's manifest definition does not match the assembly reference. (.......)

Upvotes: 0

LoLance
LoLance

Reputation: 28216

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.

Upvotes: 11

Related Questions