Commit 583f6c54 authored by yangxiaodong's avatar yangxiaodong

fix bug #44

parent 2b6c54c4
...@@ -35,3 +35,7 @@ bin/ ...@@ -35,3 +35,7 @@ bin/
/.idea /.idea
Properties Properties
/pack.bat /pack.bat
/src/DotNetCore.CAP/project.json
/src/DotNetCore.CAP/packages.config
/src/DotNetCore.CAP/DotNetCore.CAP.Net47.csproj
/NuGet.config
using System.IO; using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
...@@ -7,22 +8,30 @@ namespace Sample.RabbitMQ.SqlServer ...@@ -7,22 +8,30 @@ namespace Sample.RabbitMQ.SqlServer
{ {
public class Program public class Program
{ {
//var config = new ConfigurationBuilder()
// .AddCommandLine(args)
// .AddEnvironmentVariables("ASPNETCORE_")
// .Build();
//var host = new WebHostBuilder()
// .UseConfiguration(config)
// .UseKestrel()
// .UseContentRoot(Directory.GetCurrentDirectory())
// .UseIISIntegration()
// .UseStartup<Startup>()
// .Build();
//host.Run();
public static void Main(string[] args) public static void Main(string[] args)
{ {
var config = new ConfigurationBuilder() BuildWebHost(args).Run();
.AddCommandLine(args) }
.AddEnvironmentVariables("ASPNETCORE_")
.Build();
var host = new WebHostBuilder() public static IWebHost BuildWebHost(string[] args) =>
.UseConfiguration(config) WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>() .UseStartup<Startup>()
.Build(); .Build();
host.Run();
}
} }
} }
\ No newline at end of file
...@@ -23,7 +23,11 @@ namespace DotNetCore.CAP ...@@ -23,7 +23,11 @@ namespace DotNetCore.CAP
services.AddScoped<ICapPublisher, CapPublisher>(); services.AddScoped<ICapPublisher, CapPublisher>();
services.AddTransient<ICallbackPublisher, CapPublisher>(); services.AddTransient<ICallbackPublisher, CapPublisher>();
services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>(); services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>();
AddSqlServerOptions(services);
}
private void AddSqlServerOptions(IServiceCollection services)
{
var sqlServerOptions = new SqlServerOptions(); var sqlServerOptions = new SqlServerOptions();
_configure(sqlServerOptions); _configure(sqlServerOptions);
...@@ -32,9 +36,13 @@ namespace DotNetCore.CAP ...@@ -32,9 +36,13 @@ namespace DotNetCore.CAP
{ {
services.AddSingleton(x => services.AddSingleton(x =>
{ {
var dbContext = (DbContext)x.GetService(sqlServerOptions.DbContextType); using (var scope = x.CreateScope())
{
var provider = scope.ServiceProvider;
var dbContext = (DbContext)provider.GetService(sqlServerOptions.DbContextType);
sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
return sqlServerOptions; return sqlServerOptions;
}
}); });
} }
else else
......
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