From 583f6c5484eb0828ebe28b0885adaa96e6516003 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Tue, 5 Sep 2017 18:48:52 +0800 Subject: [PATCH 1/5] fix bug #44 --- .gitignore | 4 +++ samples/Sample.RabbitMQ.SqlServer/Program.cs | 31 ++++++++++++------- .../CAP.SqlServerCapOptionsExtension.cs | 14 +++++++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index d7fe0b1..af4f8ef 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,7 @@ bin/ /.idea Properties /pack.bat +/src/DotNetCore.CAP/project.json +/src/DotNetCore.CAP/packages.config +/src/DotNetCore.CAP/DotNetCore.CAP.Net47.csproj +/NuGet.config diff --git a/samples/Sample.RabbitMQ.SqlServer/Program.cs b/samples/Sample.RabbitMQ.SqlServer/Program.cs index 2393f73..ed4ff09 100644 --- a/samples/Sample.RabbitMQ.SqlServer/Program.cs +++ b/samples/Sample.RabbitMQ.SqlServer/Program.cs @@ -1,4 +1,5 @@ using System.IO; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -7,22 +8,30 @@ namespace Sample.RabbitMQ.SqlServer { public class Program { + + //var config = new ConfigurationBuilder() + // .AddCommandLine(args) + // .AddEnvironmentVariables("ASPNETCORE_") + // .Build(); + + //var host = new WebHostBuilder() + // .UseConfiguration(config) + // .UseKestrel() + // .UseContentRoot(Directory.GetCurrentDirectory()) + // .UseIISIntegration() + // .UseStartup() + // .Build(); + + //host.Run(); public static void Main(string[] args) { - var config = new ConfigurationBuilder() - .AddCommandLine(args) - .AddEnvironmentVariables("ASPNETCORE_") - .Build(); + BuildWebHost(args).Run(); + } - var host = new WebHostBuilder() - .UseConfiguration(config) - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - host.Run(); - } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs index 3053d76..7aac2e1 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs @@ -23,7 +23,11 @@ namespace DotNetCore.CAP services.AddScoped(); services.AddTransient(); services.AddTransient(); + AddSqlServerOptions(services); + } + private void AddSqlServerOptions(IServiceCollection services) + { var sqlServerOptions = new SqlServerOptions(); _configure(sqlServerOptions); @@ -32,9 +36,13 @@ namespace DotNetCore.CAP { services.AddSingleton(x => { - var dbContext = (DbContext)x.GetService(sqlServerOptions.DbContextType); - sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; - return sqlServerOptions; + using (var scope = x.CreateScope()) + { + var provider = scope.ServiceProvider; + var dbContext = (DbContext)provider.GetService(sqlServerOptions.DbContextType); + sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; + return sqlServerOptions; + } }); } else From a8ba769a2f7455e88763466f34b65518133012bf Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 5 Sep 2017 22:11:42 +0800 Subject: [PATCH 2/5] move controller services finder to DefaultConsumerServiceSelector --- .../CAP.ServiceCollectionExtensions.cs | 9 --------- .../IConsumerServiceSelector.Default.cs | 19 +++++++++---------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs index 06b8249..51f645b 100644 --- a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs +++ b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs @@ -82,15 +82,6 @@ namespace Microsoft.Extensions.DependencyInjection { services.AddTransient(service.Key, service.Value); } - - var types = Assembly.GetEntryAssembly().ExportedTypes; - foreach (var type in types) - { - if (Helper.IsController(type.GetTypeInfo())) - { - services.AddTransient(typeof(object), type); - } - } } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs index 4746b5c..6bf8795 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs @@ -67,17 +67,16 @@ namespace DotNetCore.CAP.Internal IServiceProvider provider) { var executorDescriptorList = new List(); - // at cap startup time, find all Controller into the DI container,the type is object. - var controllers = provider.GetServices(); - foreach (var controller in controllers) - { - var typeInfo = controller.GetType().GetTypeInfo(); - - //double check - if (!Helper.IsController(typeInfo)) continue; - executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); - } + var types = Assembly.GetEntryAssembly().ExportedTypes; + foreach (var type in types) + { + var typeInfo = type.GetTypeInfo(); + if (Helper.IsController(typeInfo)) + { + executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); + } + } return executorDescriptorList; } From b9c3a536f0548c23b5fe56aa42523c5673894ac9 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 5 Sep 2017 22:13:11 +0800 Subject: [PATCH 3/5] fixed dependency injection bugs. --- src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs | 2 +- .../CAP.RabbitMQCapOptionsExtension.cs | 3 +-- src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs | 9 +++++---- .../RabbitMQConsumerClientFactory.cs | 4 ++-- .../CAP.SqlServerCapOptionsExtension.cs | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs b/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs index 8d9bf98..5c9b6ba 100644 --- a/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs @@ -21,7 +21,7 @@ namespace DotNetCore.CAP services.AddSingleton(kafkaOptions); services.AddSingleton(); - services.AddTransient(); + services.AddSingleton(); } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs index 22e2af2..abc6d2b 100644 --- a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs @@ -23,9 +23,8 @@ namespace DotNetCore.CAP services.AddSingleton(); services.AddSingleton(); - services.AddScoped(x => x.GetService().Rent()); - services.AddTransient(); + services.AddSingleton(); } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs b/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs index 791985d..ee49ce9 100644 --- a/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs +++ b/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs @@ -10,19 +10,19 @@ namespace DotNetCore.CAP.RabbitMQ internal sealed class PublishQueueExecutor : BasePublishQueueExecutor { private readonly ILogger _logger; - private readonly IConnection _connection; + private readonly ConnectionPool _connectionPool; private readonly RabbitMQOptions _rabbitMQOptions; public PublishQueueExecutor( CapOptions options, IStateChanger stateChanger, - IConnection connection, + ConnectionPool connectionPool, RabbitMQOptions rabbitMQOptions, ILogger logger) : base(options, stateChanger, logger) { _logger = logger; - _connection = connection; + _connectionPool = connectionPool; _rabbitMQOptions = rabbitMQOptions; } @@ -30,7 +30,8 @@ namespace DotNetCore.CAP.RabbitMQ { try { - using (var channel = _connection.CreateModel()) + var connection = _connectionPool.Rent(); + using (var channel = connection.CreateModel()) { var body = Encoding.UTF8.GetBytes(content); diff --git a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs index 5fc9d8f..252b865 100644 --- a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs +++ b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs @@ -9,10 +9,10 @@ namespace DotNetCore.CAP.RabbitMQ private readonly IConnection _connection; - public RabbitMQConsumerClientFactory(RabbitMQOptions rabbitMQOptions, IConnection connection) + public RabbitMQConsumerClientFactory(RabbitMQOptions rabbitMQOptions, ConnectionPool pool) { _rabbitMQOptions = rabbitMQOptions; - _connection = connection; + _connection = pool.Rent(); } public IConsumerClient Create(string groupId) diff --git a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs index 7aac2e1..f928031 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs @@ -19,8 +19,8 @@ namespace DotNetCore.CAP public void AddServices(IServiceCollection services) { services.AddSingleton(); - services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); AddSqlServerOptions(services); From 52085b2973f6f5476c9e3ee43ee858b7e7384b21 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 5 Sep 2017 22:13:19 +0800 Subject: [PATCH 4/5] refactor. --- src/DotNetCore.CAP/IBootstrapper.Default.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/DotNetCore.CAP/IBootstrapper.Default.cs b/src/DotNetCore.CAP/IBootstrapper.Default.cs index 195916b..e4a75d5 100644 --- a/src/DotNetCore.CAP/IBootstrapper.Default.cs +++ b/src/DotNetCore.CAP/IBootstrapper.Default.cs @@ -25,14 +25,13 @@ namespace DotNetCore.CAP IOptions options, IStorage storage, IApplicationLifetime appLifetime, - IServiceProvider provider) + IEnumerable servers) { _logger = logger; _appLifetime = appLifetime; Options = options.Value; Storage = storage; - Provider = provider; - Servers = Provider.GetServices(); + Servers = servers; _cts = new CancellationTokenSource(); _ctsRegistration = appLifetime.ApplicationStopping.Register(() => @@ -55,8 +54,6 @@ namespace DotNetCore.CAP protected IEnumerable Servers { get; } - public IServiceProvider Provider { get; private set; } - public Task BootstrapAsync() { return (_bootstrappingTask = BootstrapTaskAsync()); From d646c5180e9c28bddb7a631979c4921fef0074ba Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 5 Sep 2017 22:16:35 +0800 Subject: [PATCH 5/5] set version number to 2.0.1 --- build/version.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/version.props b/build/version.props index 604d215..6c03b29 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 2 0 - 0 + 1 $(VersionMajor).$(VersionMinor).$(VersionPatch)