无法加载文件或程序集Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.1.0.0

Rob*_*ean 26 .net c# entity-framework entity-framework-core .net-core

更新到新包Microsoft.EntityFrameworkCore.SqlServer 1.1.2后,我尝试创建DBContext时出错:

System.IO.FileLoadException发生HResult = 0x80131040
Message =无法加载文件或程序集'Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.1.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(来自HRESULT的异常:0x80131040)Source = Microsoft.EntityFrameworkCore StackTrace:位于C:\ src\backend \中Services.Infrastructure.Data.SqlServerDbContext..ctor(DatabaseOptions databaseOptions)的Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions选项) Packages\Services.Infrastructure\Data\SqlServerDbContext.cs:位于C:\ src\backend\Modules\Translations\Translations.Api\Data\TranslationsDbContext.cs中Translations.Api.Data.TranslationsDbContext..ctor(DatabaseOptions databaseOptions)的第16行:第16行

我的基础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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用的数据库选项

public class DatabaseOptions
{
    public string ConnectionString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我创建上下文实例的地方

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

我的所有包都会更新.Visual Studio 2017 15.2(26430.6).在升级到1.1.2之前一切正常.请帮忙解决问题.

Mar*_*ich 40

由于您在.net框架库中使用该项目,因此自动生成的绑定重定向存在问题(可能会在即将发布的15.3 update/2.0 .net核心CLI中解决).要解决此问题,请将其添加到您的cpsroj文件中(最好在文件的任何<Import>元素之前)(.targets如果存在):

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

这应该强制MSBuild创建/更新YourProject.dll.config包含必要绑定重定向的文件.


met*_*you 10

我在下面用谷歌搜索了我的异常,它把我带到了这个 stakoverflow 帖子。

System.IO.FileNotFoundException: '无法加载文件或程序集 'Microsoft.Extensions.OptionsModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' 或其依赖项之一。该系统找不到指定的文件。'

我有相关的绑定重定向,但如果我删除所有 bin/obj 文件夹,它之后工作正常。

  • 两年前我已经+1了这个答案,它仍然相关。不知何故,每次 Windows 使用新的 .NET SDK 更新时都会发生这种情况。今天?.NET 6.0。`git clean -xfd` 就可以了。 (4认同)

alb*_*bin 7

这是一个旧线程,但在将 Azure 函数从 dotnet 核心版本 3 更新到 3.1 后,我遇到了类似的问题。

错误信息: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.9.0

在这种情况下,您需要将 .proj 文件中的 Azure 函数版本更新为“v3”。

在此处输入图片说明

  • 这对我有所帮助,但现在我需要添加一些内容,因为 Microsoft.Extensions.DependencyInjection 版本 5.x 现已可用。我不得不降级到 3.10.0,但我想任何 3.x 版本都可以正常工作。5.x 不起作用并导致相同的问题。这个问题可能会通过适用于 dotnet 5 的 Azure Functions 来解决。 (3认同)

Lui*_*eia 7

如果您在 .NET Core 中使用 Azure Functions,这将起作用

如果您使用的不是 .net 框架库,而是 .net 核心库,则接受的解决方案将不起作用,因为“您不应该在 netcoreapp 项目上设置属性 AutoGenerateBindingRedirects,因为绑定重定向甚至不是netcoreapp,它们只是 .NET Framework 中的一个概念”(来源:https : //github.com/microsoft/ApplicationInsights-dotnet/issues/1699#issuecomment-592693436)。

尝试这个:

  1. 卸载 Microsoft.Extensions.DependencyInjection.Abstractions
  2. 卸载 Microsoft.Extensions.DependencyInjection

虽然这看起来是一个激进的解决方案,但它之所以有效,是因为该库已经通过 Microsoft.Azure.Functions.Extensions 间接安装:

在此处输入图片说明

重要提示:不要尝试通过将 .NET Core 从 3.1 升级到 5.0 来解决此问题。.NET 5.0 中仍不支持 Azure 函数。补丁将于 2021 年初发布:https : //github.com/Azure/azure-functions-host/issues/6674#issuecomment-712596112

编辑:.NET Core 现在支持 Azure 函数(除非您使用的是持久实体,在这种情况下,您将不得不等待 .NET 6.0):

今天 (10/3/2021),我们宣布支持在 Azure Functions 上运行生产 .NET 5 应用程序。 https://techcommunity.microsoft.com/t5/apps-on-azure/net-on-azure-functions-roadmap/ba-p/2197916

  • 降级其他依赖项对我有用参考:https://github.com/Azure/Azure-Functions/issues/1729 (2认同)

Mat*_*att 5

我在使用 Azure 函数 3.0.13 时遇到了类似的问题

我不断收到错误消息System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.Extensions.Options, Version=6.0.0.0

我通过 NuGet 添加了对包的直接引用,然后必须将其添加到我的 project.csproj 文件中才能使一切正常工作。我不能把这个修复归功于我。不久前我在其他地方找到过它,但我不记得在哪里了。

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,但后来找到了更好的方法来保留需要的内容 `&lt;ItemGroup&gt; &lt;FunctionsPreservedDependency Include="Microsoft.Extensions.DependencyModel.dll" /&gt; &lt;/ItemGroup&gt;` (2认同)