Commit 9740e535 authored by yangxiaodong's avatar yangxiaodong

refactor of DI.

parent deee0bc7
...@@ -24,29 +24,22 @@ namespace DotNetCore.CAP ...@@ -24,29 +24,22 @@ namespace DotNetCore.CAP
services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>(); services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>();
var sqlServerOptions = new SqlServerOptions(); var sqlServerOptions = new SqlServerOptions();
_configure(sqlServerOptions); _configure(sqlServerOptions);
if (sqlServerOptions.DbContextType != null) if (sqlServerOptions.DbContextType != null)
{ {
var provider = TempBuildService(services); services.AddSingleton(x =>
var dbContextObj = provider.GetService(sqlServerOptions.DbContextType); {
var dbContext = (DbContext)dbContextObj; var dbContext = (DbContext)x.GetService(sqlServerOptions.DbContextType);
sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
return sqlServerOptions;
});
}
else
{
services.AddSingleton(sqlServerOptions);
} }
services.AddSingleton(sqlServerOptions);
}
#if NETSTANDARD1_6
private IServiceProvider TempBuildService(IServiceCollection services)
{
return services.BuildServiceProvider();
}
#else
private ServiceProvider TempBuildService(IServiceCollection services)
{
return services.BuildServiceProvider();
} }
#endif
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment