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

99,005

Solution 1

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

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 3

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 4

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

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.

Share:
99,005
Robert N. Dean
Author by

Robert N. Dean

Updated on January 22, 2022

Comments

  • Robert N. Dean
    Robert N. Dean over 2 years

    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.

  • Azhar Khorasany
    Azhar Khorasany almost 6 years
    Thanks. Solved my problem around version 2.0.0. I had version 2.1.0 installed.
  • Aaron C
    Aaron C almost 6 years
    I installed a lib version through the CLI an ai got the lattest version of the library, however, I had to downgrade the version to match my development target wich was ASP.NET Core 2.0 (Not 2.1).
  • Tigerware
    Tigerware over 5 years
    Is this only required for the database project or is it needed for all referenced projects?
  • Martin Ullrich
    Martin Ullrich over 5 years
    For all "non-sdk" ("classic") csproj files that reference .net standard assemblies. So it may be required for all projects referencing the projects that need this.
  • Kirsten
    Kirsten about 4 years
    Does 15.3 refer to Visual Studio here? Any idea when it will be fixed? ~stackoverflow.com/questions/60217182/…
  • Oxymoron
    Oxymoron over 3 years
    This helped me out, but I need to add something now that Version 5.x of Microsoft.Extensions.DependencyInjection is available now. I had to downgrade to 3.10.0, but I imagine any 3.x version will work just as well. 5.x does not work and results in the same issue. This will probably be resolved with Azure Functions for dotnet 5 is availalbe.
  • Leon
    Leon over 3 years
    After lots of research and finally I fixed this issue, see my answer below: stackoverflow.com/a/65434701/1815500
  • Tomasz Kaniewski
    Tomasz Kaniewski about 3 years
    Downgrading other dependencies works for me ref: github.com/Azure/Azure-Functions/issues/1729
  • Michael
    Michael about 3 years
    Thanks, fix worked for me after adding DI to an Azure Function
  • pasx
    pasx almost 3 years
    Microsoft Visual Studio Professional 2019 - Version 16.8.3. The UI shows this property checked even if the tag it is not present in the csproj. Adding the AutoGenerateBindingRedirects tag in the csproj, the UI is then capable of setting it. It did not solve my binding issues though...
  • Rohim Chou
    Rohim Chou over 2 years
    In my case also this Microsoft.Extensions.Primitives assembly couldn't be loaded. Execute Update-Package -reinstall -Project MyProject from Package Manager Console resolved the issue...
  • jsgoupil
    jsgoupil about 2 years
    I had +1 this answer 2 years ago and it's still relevant. And somehow, it happens every time Windows updates with a new .NET SDK. Today? .NET 6.0. git clean -xfd does the trick.