Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

C#.NetEntity Framework.Net CoreEntity Framework-Core

C# Problem Overview


After update to the new package Microsoft.EntityFrameworkCore.SqlServer 1.1.2 I got error when try to create DBContext:

> System.IO.FileLoadException occurred HResult=0x80131040
> Message=Could not load file or assembly > 'Microsoft.Extensions.DependencyInjection.Abstractions, > Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or > one of its dependencies. The located assembly's manifest definition > does not match the assembly reference. (Exception from HRESULT: > 0x80131040) Source=Microsoft.EntityFrameworkCore StackTrace: at > Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions > options) at > Services.Infrastructure.Data.SqlServerDbContext..ctor(DatabaseOptions > databaseOptions) in > C:\src\backend\Packages\Services.Infrastructure\Data\SqlServerDbContext.cs:line > 16 at > Translations.Api.Data.TranslationsDbContext..ctor(DatabaseOptions > databaseOptions) in > C:\src\backend\Modules\Translations\Translations.Api\Data\TranslationsDbContext.cs:line > 16

My base DbContext

public class SqlServerDbContext : DbContext
{
	private readonly DatabaseOptions _databaseOptions;

	protected SqlServerDbContext(DatabaseOptions databaseOptions)
	{
		if (string.IsNullOrEmpty(databaseOptions.ConnectionString))
			throw new Exception("Database connection string is missed.");

		_databaseOptions = databaseOptions;
	}

	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		optionsBuilder.UseSqlServer(_databaseOptions.ConnectionString);
	}
}

Database options that I use

public class DatabaseOptions
{
	public string ConnectionString { get; set; }
}

Place where I create instance of context

 var dbOptions = new DatabaseOptions { ConnectionString = _connectionString };
 DbContext = (TContext) Activator.CreateInstance(typeof(TContext), dbOptions);
// where TContext is derived class from SqlServerDbContext

All my packages are updated. Visual Studio 2017 15.2 (26430.6). Before upgrade to 1.1.2 everything works fine. Please help to solve the problem.

C# Solutions


Solution 1 - C#

Since you're using the project in a .net framework library, there's an issue with auto-generated binding redirects (might be resolved in the upcoming 15.3 update / 2.0 .net core CLI). To work around it, add this in your cpsroj file (preferably before any <Import> element for a .targets file if present):

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

This should force MSBuild to create / update a YourProject.dll.config file containing the necessary binding redirects.

Solution 2 - C#

I Googled my exception below, and it brought me to this stakoverflow post.

> System.IO.FileNotFoundException: 'Could not load file or assembly > 'Microsoft.Extensions.OptionsModel, Version=1.0.0.0, Culture=neutral, > PublicKeyToken=adb9793829ddae60' or one of its dependencies. The > system cannot find the file specified.'

I had the relevant Binding Redirects but if I Delete all bin/obj folders it worked fine afterwards.

Solution 3 - C#

If you're working with Azure Functions in .NET Core this will work

If you're not using the .net framework library, but the .net core library, the accepted solution will not work because "you shouldn't be setting the property AutoGenerateBindingRedirects on a netcoreapp project as binding redirects aren't even a thing in netcoreapp, they are only a concept in .NET Framework" (source: https://github.com/microsoft/ApplicationInsights-dotnet/issues/1699#issuecomment-592693436).

Try this:

  1. Uninstall Microsoft.Extensions.DependencyInjection.Abstractions
  2. Uninstall Microsoft.Extensions.DependencyInjection

Although this seems like a radical solution, it works because the library is already indirectly installed through Microsoft.Azure.Functions.Extensions:

enter image description here

Important: Do not try to solve this issue by upgrading .NET Core from 3.1 to 5.0. Azure functions are still not supported in .NET 5.0. A patch is coming in early 2021: https://github.com/Azure/azure-functions-host/issues/6674#issuecomment-712596112

EDIT: Azure functions are now supported in .NET Core (unless you're using durable entities, in that case you'll have to wait for .NET 6.0):

Today (10/3/2021), we announced support for running production .NET 5 apps on Azure Functions. https://techcommunity.microsoft.com/t5/apps-on-azure/net-on-azure-functions-roadmap/ba-p/2197916

Solution 4 - C#

This is an old thread but I had a similar same issue after I updated my Azure function from dotnet core version 3 to 3.1.

Error message: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.9.0

In this case you need to update the Azure function version to 'v3' in .proj file.

enter image description here

Solution 5 - C#

For me, what did it was to downgrade all the packages support .Net Standard to 2.2.0 from 3.x.x i guess the 3.x packages relevant to a different version of .Net standard which does not support ,net framework.

Solution 6 - C#

I was having this problem. But the packet was Microsoft.Extensions.Primitives. In my case I installed it in my Project and in my Project test from nuget. Maybe could help someone.

Solution 7 - C#

It is a cracy thing, but for me what really worked was upating all packages that begins with Microsoft, since I updated my project to run over .Net 4.7.2, Then I just right click on Solution and Clean and Build again.

Solution 8 - C#

I've been getting a similar issue using Azure functions 3.0.13

I kept getting the error System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Extensions.Options, Version=6.0.0.0

I added a direct reference to the package via NuGet and then had to add this to my project.csproj file to make everything work. I can't take credit for this fix. I found it somewhere else a while ago, but I can't recall from where.

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

Solution 9 - C#

If Luis Gouveia solution doesn't work for you, try downgrading other dependencies included in the project. There are some compatibility issues with .NET standard.

Ref: https://github.com/Azure/Azure-Functions/issues/1729

Solution 10 - C#

I had same problem and finally fixed with below solution

  1. Install "System.ComponentModel.Annotations" version 4.4.1 in a temporary project to get the file of "System.ComponentModel.Annotations.dll", then copy this file to your real project and then change your project reference to point this file instead of the original NuGet path

  2. Then in your web.config add below code that redirec

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <publisherPolicy apply="no" />
      <dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.2.1.0" />
      </dependentAssembly>
    </assemblyBinding>

Solution 11 - C#

I was working on my simple .Net 5.0 Console app and while modifying my project's namespace the "auto-installation" added only the Microsoft.Extensions.DependencyInjection.Abstraction not the package nuget Microsoft.Extensions.DependencyInjection

It took me a while to realise that...

Solution 12 - C#

I had the same problem with a .Net Framework website that included an assembly using EFCore 3.1.22. I resolved the problem by putting the assembly redirects in the web.config.

Solution 13 - C#

Here some solves :

1-I updated my packages from NuGet to latest version and working 100%.

2- make these editing in config :

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>

3-Delete all bin and objct folders then clean solution and finally run , hope this will be work .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert N. DeanView Question on Stackoverflow
Solution 1 - C#Martin UllrichView Answer on Stackoverflow
Solution 2 - C#metoyouView Answer on Stackoverflow
Solution 3 - C#Luis GouveiaView Answer on Stackoverflow
Solution 4 - C#albinView Answer on Stackoverflow
Solution 5 - C#Yosi GolanView Answer on Stackoverflow
Solution 6 - C#Edvan SouzaView Answer on Stackoverflow
Solution 7 - C#RubenView Answer on Stackoverflow
Solution 8 - C#MattView Answer on Stackoverflow
Solution 9 - C#Tomasz KaniewskiView Answer on Stackoverflow
Solution 10 - C#LeonView Answer on Stackoverflow
Solution 11 - C#Mister QView Answer on Stackoverflow
Solution 12 - C#stevie_cView Answer on Stackoverflow
Solution 13 - C#mandarin softwareView Answer on Stackoverflow