DependencyInjection 找不到程序集
DependencyInjection cannot find the assembly
我有两个 dll WebApp 和 MyUtils.EntityFramework 它们都在同一目录中并且 WebApp 引用 MyUtils.EntityFramework.
这是 WebApp 的样子
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 的样子
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");
}
}
这是我因为这一行而得到的错误
builder.ReplaceService<IHistoryRepository, MigrationHistoryRepository<DerivedContext1>>();
bin 输出包含两个 dll,所以我不明白问题出在哪里(我使用的是 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被多次编译,这里更详细How to organize multiple git packages in dotnet core
我有两个 dll WebApp 和 MyUtils.EntityFramework 它们都在同一目录中并且 WebApp 引用 MyUtils.EntityFramework.
这是 WebApp 的样子
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 的样子
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");
}
}
这是我因为这一行而得到的错误
builder.ReplaceService<IHistoryRepository, MigrationHistoryRepository<DerivedContext1>>();
bin 输出包含两个 dll,所以我不明白问题出在哪里(我使用的是 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被多次编译,这里更详细How to organize multiple git packages in dotnet core