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

19

89

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.

Wily answered 16/5, 2017 at 7:49 Comment(5)
Please give information about what the projects is you're consuming the package from? .net standard / .net framework library / console / web application?Courageous
@MartinUllrich .net framework library with .net461Wily
FWIW - This can happen if you played with upgrading your NuGets, then decided to use your source control tools to revert back to your 'working' project (reverted packages.config and the .csproj file) but then forgot to revert the binding redirects out of the web.configBailor
It would be great if this error contained helpful information, such as the incompatible packages, and at least a more clear error message.Intercollegiate
I noticed in the error the Microsoft.Extensions.DependencyInjection.Abstractions 2.0.0 version was requested. Downgraded to that package in NuGet, cleared the NuGet cache. Also cleared all binding redirects in web.config to have VisualStudio update the config clicking each compiler warning.Hebraist
C
105

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.

Courageous answered 16/5, 2017 at 8:38 Comment(7)
Thanks. Solved my problem around version 2.0.0. I had version 2.1.0 installed.Insectile
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).Abracadabra
Is this only required for the database project or is it needed for all referenced projects?Eugenol
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.Courageous
Does 15.3 refer to Visual Studio here? Any idea when it will be fixed? ~#60217682Catabolism
After lots of research and finally I fixed this issue, see my answer below: https://mcmap.net/q/236249/-could-not-load-file-or-assembly-microsoft-extensions-dependencyinjection-abstractions-version-1-1-0-0Amianthus
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...Weltanschauung
U
18

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

Uzial answered 3/1, 2021 at 14:49 Comment(3)
Downgrading other dependencies works for me ref: github.com/Azure/Azure-Functions/issues/1729Carcassonne
Thanks, fix worked for me after adding DI to an Azure FunctionBolt
Hmm, what do you do if you are getting this error and you don't have either of those DependencyInjection extensions installed 🤔Septennial
I
15

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.

Incommunicado answered 30/11, 2019 at 13:20 Comment(1)
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.Fascination
R
9

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

Ronnieronny answered 15/10, 2020 at 5:22 Comment(2)
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.Subreption
What to do here ? Microsoft.Extensions.DependencyInjection 7.0.0Rummage
T
5

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>
Twotime answered 11/1, 2022 at 3:15 Comment(1)
This worked for me but later found better approach to just preserve what is need <ItemGroup> <FunctionsPreservedDependencies Include="Microsoft.Extensions.DependencyModel.dll" /> </ItemGroup>Crowther
J
4

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.

Janayjanaya answered 10/10, 2020 at 18:3 Comment(2)
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...Dorison
I had problem with Microsoft.Extensions.Options while unit testing after upgrading a nuget package. Resolved when I added that same package into my Test project (part of the same solution), even though it wasn't directly used. I then removed the package from the Test project and it still worked.Quote
G
2

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.

Glop answered 22/6, 2020 at 12:34 Comment(0)
E
2

It is a crazy thing, but what really worked for me was updating all packages that begin with "Microsoft", because I updated my project to run over .Net 4.7.2. Then, I just right-clicked on "Solution", selected "Clean Solution" and "Rebuild Solution".

Electronarcosis answered 28/9, 2021 at 13:46 Comment(0)
C
1

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

Carcassonne answered 1/2, 2021 at 10:41 Comment(0)
M
1

I got this error message in my .NET 6 Azure Function App:

Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

As this was the first page that came up when googling the issue, I thought it might help posting my solution. All upgrading, downgrading and re-installing of nuget-packages was unsuccessful. I eventually stumbled across some MS documentation where it says

If you re-target a project to a different version of .NET, your references may not resolve properly in some cases. Explicit fully qualified references to assemblies often cause this issue, but you can resolve it by removing the references that don't resolve and then adding them back to the project.

This was the line in my project file causing the problem:

<PackageReference Include="Microsoft.Extensions.Primitives" Version="8.0.0-preview.4.23259.5" />

Removing the version solved the issue:

<PackageReference Include="Microsoft.Extensions.Primitives" />
Marital answered 12/6, 2023 at 15:7 Comment(0)
F
0

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 .

Farrington answered 10/10, 2020 at 2:15 Comment(0)
A
0

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>
Amianthus answered 24/12, 2020 at 6:19 Comment(0)
J
0

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

Janenejanenna answered 4/5, 2021 at 15:6 Comment(0)
R
0

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.

Roybal answered 21/1, 2022 at 10:48 Comment(0)
G
0

Deleting the "app.config" file in the project folder solved the problem. It contained references to a different version of the file, while it should have been a certain one given the Nuget package setup I had.

These *.config files are auto-generated, and the program runs without them. The "packages.config" is different.

Always beware when these config files exist in your project, and treat them with due suspicion.

Gunderson answered 8/8, 2023 at 12:16 Comment(0)
P
0

I had to upgrade the package in the NuGetPackage Manager and it worked for me

System.IO.FileLoadException
  HResult=0x80131040
  Message=Could not load file or assembly 'Microsoft.Extensions.Primitives, Version=7.0.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.Extensions.Configuration
  StackTrace:
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeHostConfiguration()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at Data_Export_Refinity.App..ctor() in C:\Users\Nathaniel\source\repos\........\App.xaml.cs:line 25
   at Data_Export_Refinity.App.Main() in C:\Users\Nathaniel\source\repos\..........\obj\Debug\App.g.cs:line 49

enter image description here

Pod answered 1/9, 2023 at 16:27 Comment(0)
M
0

Almost 7 years passed, but this problem is still relevant. Why? Because it's common when you upgrade your project from .NET Framework to modern .NET. At my company, we are upgrading very smoothly step by step, not just like "the next release is going to be new .NET release". No, it does not work for big projects at big companies.

We had .NET Framework 4.8 and started to upgrade the solution project by project. Firstly, we moved several projects to netstandard2.0. And when it came to database we met this error.

We use EF Core 3.1 because this is the only version that works with netstandard2.0. Our goal was to move projects that are responsible for DB interaction to netstandard2.0 and then use them from .NET Framework projects. Why am I describing all of this? Because there is pretty high probability that here you will have similar problem if you are in the situation like we have. Next I will show all the changes that I have added to avoid this.

web.config

<dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Caching.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.9.0" newVersion="3.1.9.0" />
      </dependentAssembly>

Your main web api .csproj

<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.9" />


<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
the last pacakge was required in my case. Maybe in your case it's not necesary.

.csproj of the project that interacts with DB

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Data.SqlClient.SNI" Version="1.1.1" />
        <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.9" />
        <PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>

        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.9" />
    </ItemGroup>

// here are project reference (models or something)

Marguritemargy answered 2/2 at 8:33 Comment(0)
T
0

I encountered the same issue and I resolved it by downgrading "Microsoft.Extensions.Http" to 6.0.0.0 to match my TargetFramework net6.0

Trustless answered 6/2 at 18:23 Comment(0)
B
0

This drove me crazy. It only happened when I deployed to an Azure App Service and it only happened on that specific service. I could deploy it elsewhere and it would run fine. What I ended up doing was going into the Kudu console (yourappservicename.scm.azurewebsites.net) and deleting the folders:

  • home\site\wwwroot\*
  • home\site\repository\*
  • home\site\deployments\*

I then re-deployed and everything worked.

Berky answered 8/5 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.