diff --git a/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs b/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs index 3d29fa1..a0bf29d 100644 --- a/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.MySql/CAP.MySqlCapOptionsExtension.cs @@ -25,22 +25,32 @@ namespace DotNetCore.CAP services.AddScoped(); services.AddTransient(); + AddSingletionMySqlOptions(services); + } + + private void AddSingletionMySqlOptions(IServiceCollection services) + { var mysqlOptions = new MySqlOptions(); + _configure(mysqlOptions); if (mysqlOptions.DbContextType != null) + { services.AddSingleton(x => { using (var scope = x.CreateScope()) { var provider = scope.ServiceProvider; - var dbContext = (DbContext) provider.GetService(mysqlOptions.DbContextType); + var dbContext = (DbContext)provider.GetService(mysqlOptions.DbContextType); mysqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; return mysqlOptions; } }); + } else + { services.AddSingleton(mysqlOptions); + } } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.MySql/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.MySql/CAP.Options.Extensions.cs index 5f66f6c..9993c16 100644 --- a/src/DotNetCore.CAP.MySql/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.MySql/CAP.Options.Extensions.cs @@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection { if (configure == null) throw new ArgumentNullException(nameof(configure)); + options.RegisterExtension(new MySqlCapOptionsExtension(configure)); return options; @@ -32,10 +33,11 @@ namespace Microsoft.Extensions.DependencyInjection { if (configure == null) throw new ArgumentNullException(nameof(configure)); - var efOptions = new EFOptions {DbContextType = typeof(TContext)}; - configure(efOptions); - - options.RegisterExtension(new MySqlCapOptionsExtension(configure)); + options.RegisterExtension(new MySqlCapOptionsExtension(x => + { + configure(x); + x.DbContextType = typeof(TContext); + })); return options; } diff --git a/src/DotNetCore.CAP.PostgreSql/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.PostgreSql/CAP.Options.Extensions.cs index 9fde846..e001c05 100644 --- a/src/DotNetCore.CAP.PostgreSql/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.PostgreSql/CAP.Options.Extensions.cs @@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection public static CapOptions UseEntityFramework(this CapOptions options) where TContext : DbContext { - return options.UseEntityFramework(opt => { opt.DbContextType = typeof(TContext); }); + return options.UseEntityFramework(opt => { }); } public static CapOptions UseEntityFramework(this CapOptions options, Action configure) @@ -32,10 +32,11 @@ namespace Microsoft.Extensions.DependencyInjection { if (configure == null) throw new ArgumentNullException(nameof(configure)); - var efOptions = new EFOptions {DbContextType = typeof(TContext)}; - configure(efOptions); - - options.RegisterExtension(new PostgreSqlCapOptionsExtension(configure)); + options.RegisterExtension(new PostgreSqlCapOptionsExtension(x => + { + configure(x); + x.DbContextType = typeof(TContext); + })); return options; } diff --git a/src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs b/src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs index e2d4a14..f1e94f8 100644 --- a/src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlCapOptionsExtension.cs @@ -25,10 +25,16 @@ namespace DotNetCore.CAP services.AddScoped(); services.AddTransient(); + AddSingletonPostgreSqlOptions(services); + } + + private void AddSingletonPostgreSqlOptions(IServiceCollection services) + { var postgreSqlOptions = new PostgreSqlOptions(); _configure(postgreSqlOptions); if (postgreSqlOptions.DbContextType != null) + { services.AddSingleton(x => { using (var scope = x.CreateScope()) @@ -39,8 +45,11 @@ namespace DotNetCore.CAP return postgreSqlOptions; } }); + } else + { services.AddSingleton(postgreSqlOptions); + } } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.SqlServer/CAP.Options.Extensions.cs b/src/DotNetCore.CAP.SqlServer/CAP.Options.Extensions.cs index c977921..c882165 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.Options.Extensions.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.Options.Extensions.cs @@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection public static CapOptions UseEntityFramework(this CapOptions options) where TContext : DbContext { - return options.UseEntityFramework(opt => { opt.DbContextType = typeof(TContext); }); + return options.UseEntityFramework(opt => { }); } public static CapOptions UseEntityFramework(this CapOptions options, Action configure) @@ -32,10 +32,11 @@ namespace Microsoft.Extensions.DependencyInjection { if (configure == null) throw new ArgumentNullException(nameof(configure)); - var efOptions = new EFOptions {DbContextType = typeof(TContext)}; - configure(efOptions); - - options.RegisterExtension(new SqlServerCapOptionsExtension(configure)); + options.RegisterExtension(new SqlServerCapOptionsExtension(x => + { + configure(x); + x.DbContextType = typeof(TContext); + })); return options; } diff --git a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs index ba17ff0..55408bd 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs @@ -24,6 +24,7 @@ namespace DotNetCore.CAP services.AddScoped(); services.AddScoped(); services.AddTransient(); + AddSqlServerOptions(services); } @@ -34,18 +35,22 @@ namespace DotNetCore.CAP _configure(sqlServerOptions); if (sqlServerOptions.DbContextType != null) + { services.AddSingleton(x => { using (var scope = x.CreateScope()) { var provider = scope.ServiceProvider; - var dbContext = (DbContext) provider.GetService(sqlServerOptions.DbContextType); + var dbContext = (DbContext)provider.GetService(sqlServerOptions.DbContextType); sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; return sqlServerOptions; } }); + } else + { services.AddSingleton(sqlServerOptions); + } } } } \ No newline at end of file