DependencyInjection找不到程序集

DependencyInjection找不到程序集

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

问题描述

我有两个dlls 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已多次编译,此处有更多详细信息

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

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

08-20 13:56