DependencyInjection 找不到程序集 [英] DependencyInjection cannot find the assembly

查看:29
本文介绍了DependencyInjection 找不到程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 dll WebApp 和 MyUtils.EntityFramework,它们都在同一目录中,WebApp 引用了 MyUtils.EntityFramework.

I have a two dlls WebApp and MyUtils.EntityFramework they are both in the same directory and WebApp references MyUtils.EntityFramework.

这就是 WebApp 的样子

This is how WebApp looks

public class Program
{
    public static void Main()
    {
        var appServiceProivder = new ServiceCollection()
            .AddDbContext<DerivedContext1>()
            .BuildServiceProvider();

        using (var serviceScope = appServiceProivder
            .GetRequiredService<IServiceScopeFactory>()
            .CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetService<DerivedContext1>();

            Debug.Assert(context.GetService<IDbContextOptions>()
                .GetType() == typeof(DbContextOptions<DerivedContext1>));
        }
    }
}

[DbSchema("test")]
public class DerivedContext1 : DbContext
{
    public DerivedContext1(DbContextOptions options) : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        builder.UseSqlServer("secret connection string");
        builder.ReplaceService<IHistoryRepository, 
            MigrationHistoryRepository<DerivedContext1>>();
    }
}

这就是 MyUtils.EntityFramework 的样子

this is how MyUtils.EntityFramework looks

public class DbSchemaAttribute : Attribute
{
    public DbSchemaAttribute(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

public class MigrationHistoryRepository<TDbContext>
    : SqlServerHistoryRepository where TDbContext : DbContext
{
    public MigrationHistoryRepository(HistoryRepositoryDependencies dependencies)
        : base(dependencies)
    {
    }

    protected override string TableName { get { return "migration_history"; } }

    protected override string TableSchema
    {
        get
        {
            var dsSchemaAttr = typeof(TDbContext)
                .GetCustomAttributes(typeof(DbSchemaAttribute), true)
                    .SingleOrDefault() as DbSchemaAttribute;

            if (dsSchemaAttr != null)
                return dsSchemaAttr.Name;

            return null;
        }
    }

    protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
    {
        base.ConfigureTable(history);

        history.HasKey(h => h.MigrationId).HasName($"{TableName}_pkey");
        history.Property(h => h.MigrationId).HasColumnName("id");
        history.Property(h => h.ProductVersion).HasColumnName("product_version");
    }
}

这是我因为这条线得到的错误

and this is the error I get because of this line

builder.ReplaceService<IHistoryRepository, MigrationHistoryRepository<DerivedContext1>>();

bin 输出包含两个 dll,所以我不明白问题是什么(我使用的是 Fedora 33)

bin output contains both dlls so I don't understand what the problem is (I'm using fedora 33)

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.EntityFrameworkCore.dll: 'Could not load file or assembly 'MyUtils.EntityFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
   at WebApp.DerivedContext1.OnConfiguring(DbContextOptionsBuilder builder) in /src/api/Program.cs:line 92
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at WebApp.Program.Main() in /src/api/Program.cs:line 78

推荐答案

因为 efcore utils 编译多次,这里有更多细节 如何在dotnet核心中组织多个git包

It's because efcore utils is compiled multiple times, here is more detail How to organize multiple git packages in dotnet core

这篇关于DependencyInjection 找不到程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆