From 583f6c5484eb0828ebe28b0885adaa96e6516003 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Tue, 5 Sep 2017 18:48:52 +0800 Subject: [PATCH 1/9] 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 d7fe0b122..af4f8ef4a 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 2393f7382..ed4ff0972 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 3053d76f0..7aac2e18f 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs @@ -23,7 +23,11 @@ public void AddServices(IServiceCollection services) services.AddScoped(); services.AddTransient(); services.AddTransient(); + AddSqlServerOptions(services); + } + private void AddSqlServerOptions(IServiceCollection services) + { var sqlServerOptions = new SqlServerOptions(); _configure(sqlServerOptions); @@ -32,9 +36,13 @@ public void AddServices(IServiceCollection services) { 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/9] 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 06b824913..51f645baa 100644 --- a/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs +++ b/src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs @@ -82,15 +82,6 @@ private static void AddSubscribeServices(IServiceCollection services) { 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 4746b5c4e..6bf879548 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs @@ -67,17 +67,16 @@ private static IEnumerable FindConsumersFromControll 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/9] 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 8d9bf9860..5c9b6ba98 100644 --- a/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.Kafka/CAP.KafkaCapOptionsExtension.cs @@ -21,7 +21,7 @@ public void AddServices(IServiceCollection services) 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 22e2af27c..abc6d2bd1 100644 --- a/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.RabbitMQ/CAP.RabbitMQCapOptionsExtension.cs @@ -23,9 +23,8 @@ public void AddServices(IServiceCollection services) 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 791985dad..ee49ce91d 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 @@ public override Task PublishAsync(string keyName, string content) { 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 5fc9d8f82..252b865df 100644 --- a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs +++ b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs @@ -9,10 +9,10 @@ internal sealed class RabbitMQConsumerClientFactory : IConsumerClientFactory 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 7aac2e18f..f928031b3 100644 --- a/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs +++ b/src/DotNetCore.CAP.SqlServer/CAP.SqlServerCapOptionsExtension.cs @@ -19,8 +19,8 @@ public SqlServerCapOptionsExtension(Action configure) 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/9] 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 195916b5b..e4a75d5fd 100644 --- a/src/DotNetCore.CAP/IBootstrapper.Default.cs +++ b/src/DotNetCore.CAP/IBootstrapper.Default.cs @@ -25,14 +25,13 @@ public DefaultBootstrapper( 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 @@ public DefaultBootstrapper( 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/9] 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 604d2156c..6c03b29d5 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 2 0 - 0 + 1 $(VersionMajor).$(VersionMinor).$(VersionPatch) From 975fcb7de54122cb263addbf93d6c59e29b24166 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 7 Sep 2017 20:54:22 +0800 Subject: [PATCH 6/9] fixed not return connection to pool bug. --- src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs | 2 +- src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs | 7 ++++++- .../RabbitMQConsumerClient.cs | 13 ++++++++----- .../RabbitMQConsumerClientFactory.cs | 6 +++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs b/src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs index 83116bf27..5db81e7d3 100644 --- a/src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs +++ b/src/DotNetCore.CAP.RabbitMQ/ConnectionPool.cs @@ -8,7 +8,7 @@ namespace DotNetCore.CAP.RabbitMQ { public class ConnectionPool : IConnectionPool, IDisposable { - private const int DefaultPoolSize = 32; + private const int DefaultPoolSize = 15; private readonly ConcurrentQueue _pool = new ConcurrentQueue(); diff --git a/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs b/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs index ee49ce91d..0a4750136 100644 --- a/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs +++ b/src/DotNetCore.CAP.RabbitMQ/PublishQueueExecutor.cs @@ -28,9 +28,10 @@ public PublishQueueExecutor( public override Task PublishAsync(string keyName, string content) { + var connection = _connectionPool.Rent(); + try { - var connection = _connectionPool.Rent(); using (var channel = connection.CreateModel()) { var body = Encoding.UTF8.GetBytes(content); @@ -56,6 +57,10 @@ public override Task PublishAsync(string keyName, string content) Description = ex.Message })); } + finally + { + _connectionPool.Return(connection); + } } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs index a6720fdc1..0172c2be0 100644 --- a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs +++ b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs @@ -14,7 +14,7 @@ internal sealed class RabbitMQConsumerClient : IConsumerClient private readonly string _queueName; private readonly RabbitMQOptions _rabbitMQOptions; - private IConnection _connection; + private ConnectionPool _connectionPool; private IModel _channel; private ulong _deliveryTag; @@ -23,11 +23,11 @@ internal sealed class RabbitMQConsumerClient : IConsumerClient public event EventHandler OnError; public RabbitMQConsumerClient(string queueName, - IConnection connection, + ConnectionPool connectionPool, RabbitMQOptions options) { _queueName = queueName; - _connection = connection; + _connectionPool = connectionPool; _rabbitMQOptions = options; _exchageName = options.TopicExchangeName; @@ -36,7 +36,9 @@ public RabbitMQConsumerClient(string queueName, private void InitClient() { - _channel = _connection.CreateModel(); + var connection = _connectionPool.Rent(); + + _channel = connection.CreateModel(); _channel.ExchangeDeclare( exchange: _exchageName, @@ -49,6 +51,8 @@ private void InitClient() exclusive: false, autoDelete: false, arguments: arguments); + + _connectionPool.Return(connection); } public void Subscribe(IEnumerable topics) @@ -81,7 +85,6 @@ public void Commit() public void Dispose() { _channel.Dispose(); - _connection.Dispose(); } private void OnConsumerReceived(object sender, BasicDeliverEventArgs e) diff --git a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs index 252b865df..753fc0565 100644 --- a/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs +++ b/src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClientFactory.cs @@ -6,18 +6,18 @@ namespace DotNetCore.CAP.RabbitMQ internal sealed class RabbitMQConsumerClientFactory : IConsumerClientFactory { private readonly RabbitMQOptions _rabbitMQOptions; - private readonly IConnection _connection; + private readonly ConnectionPool _connectionPool; public RabbitMQConsumerClientFactory(RabbitMQOptions rabbitMQOptions, ConnectionPool pool) { _rabbitMQOptions = rabbitMQOptions; - _connection = pool.Rent(); + _connectionPool = pool; } public IConsumerClient Create(string groupId) { - return new RabbitMQConsumerClient(groupId, _connection, _rabbitMQOptions); + return new RabbitMQConsumerClient(groupId, _connectionPool, _rabbitMQOptions); } } } \ No newline at end of file From 725a3a45f233b55cf7fb470aed4f354039f324e8 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Mon, 11 Sep 2017 15:41:36 +0800 Subject: [PATCH 7/9] refactor and remove reference of IServiceProvider --- src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs | 5 ++--- src/DotNetCore.CAP/IConsumerHandler.Default.cs | 2 +- .../Internal/IConsumerServiceSelector.Default.cs | 6 +++--- src/DotNetCore.CAP/Internal/MethodMatcherCache.cs | 5 ++--- test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs | 4 ++-- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs b/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs index b9e1e0372..e7985d780 100644 --- a/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs +++ b/src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs @@ -11,10 +11,9 @@ public interface IConsumerServiceSelector /// /// Selects a set of candidates for the current message associated with /// . - /// - /// . + /// /// A set of candidates or null. - IReadOnlyList SelectCandidates(IServiceProvider provider); + IReadOnlyList SelectCandidates(); /// /// Selects the best candidate from for the diff --git a/src/DotNetCore.CAP/IConsumerHandler.Default.cs b/src/DotNetCore.CAP/IConsumerHandler.Default.cs index 2990e8b0c..4e69e12a9 100644 --- a/src/DotNetCore.CAP/IConsumerHandler.Default.cs +++ b/src/DotNetCore.CAP/IConsumerHandler.Default.cs @@ -47,7 +47,7 @@ public ConsumerHandler( public void Start() { - var groupingMatchs = _selector.GetCandidatesMethodsOfGroupNameGrouped(_serviceProvider); + var groupingMatchs = _selector.GetCandidatesMethodsOfGroupNameGrouped(); foreach (var matchGroup in groupingMatchs) { diff --git a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs index 6bf879548..2f4269044 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs @@ -33,13 +33,13 @@ public ConsumerExecutorDescriptor SelectBestCandidate(string key, return executeDescriptor.FirstOrDefault(x => x.Attribute.Name == key); } - public IReadOnlyList SelectCandidates(IServiceProvider provider) + public IReadOnlyList SelectCandidates() { var executorDescriptorList = new List(); - executorDescriptorList.AddRange(FindConsumersFromInterfaceTypes(provider)); + executorDescriptorList.AddRange(FindConsumersFromInterfaceTypes(_serviceProvider)); - executorDescriptorList.AddRange(FindConsumersFromControllerTypes(provider)); + executorDescriptorList.AddRange(FindConsumersFromControllerTypes(_serviceProvider)); return executorDescriptorList; } diff --git a/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs b/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs index 332158b85..db59fcc8f 100644 --- a/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs +++ b/src/DotNetCore.CAP/Internal/MethodMatcherCache.cs @@ -22,12 +22,11 @@ public MethodMatcherCache(IConsumerServiceSelector selector) /// Get a dictionary of candidates.In the dictionary, /// the Key is the CAPSubscribeAttribute Group, the Value for the current Group of candidates /// - /// - public ConcurrentDictionary> GetCandidatesMethodsOfGroupNameGrouped(IServiceProvider provider) + public ConcurrentDictionary> GetCandidatesMethodsOfGroupNameGrouped() { if (Entries.Count != 0) return Entries; - var executorCollection = _selector.SelectCandidates(provider); + var executorCollection = _selector.SelectCandidates(); var groupedCandidates = executorCollection.GroupBy(x => x.Attribute.Group); diff --git a/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs b/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs index 5ed56e1fe..7704e88c9 100644 --- a/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs +++ b/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs @@ -25,7 +25,7 @@ public ConsumerServiceSelectorTest() public void CanFindAllConsumerService() { var selector = _provider.GetRequiredService(); - var candidates = selector.SelectCandidates(_provider); + var candidates = selector.SelectCandidates(); Assert.Equal(2, candidates.Count); } @@ -34,7 +34,7 @@ public void CanFindAllConsumerService() public void CanFindSpecifiedTopic() { var selector = _provider.GetRequiredService(); - var candidates = selector.SelectCandidates(_provider); + var candidates = selector.SelectCandidates(); var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates); Assert.NotNull(bestCandidates); From 756935abfbbee66bd72403ca207e6225b8566168 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 12 Sep 2017 20:52:17 +0800 Subject: [PATCH 8/9] fixed .net core 2.0 services resolve bug --- .../IConsumerServiceSelector.Default.cs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs index 6bf879548..24c04dbaf 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs @@ -48,19 +48,23 @@ private static IEnumerable FindConsumersFromInterfac IServiceProvider provider) { var executorDescriptorList = new List(); - - var consumerServices = provider.GetServices(); - foreach (var service in consumerServices) + + using (var scoped = provider.CreateScope()) { - var typeInfo = service.GetType().GetTypeInfo(); - if (!typeof(ICapSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo)) + var scopedProvider = scoped.ServiceProvider; + var consumerServices = scopedProvider.GetServices(); + foreach (var service in consumerServices) { - continue; - } + var typeInfo = service.GetType().GetTypeInfo(); + if (!typeof(ICapSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo)) + { + continue; + } - executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); + executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); + } + return executorDescriptorList; } - return executorDescriptorList; } private static IEnumerable FindConsumersFromControllerTypes( @@ -76,7 +80,7 @@ private static IEnumerable FindConsumersFromControll { executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); } - } + } return executorDescriptorList; } From 53f72d8ec4242d7302bd59a5f8a8d0cc9ad6d209 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 12 Sep 2017 21:08:50 +0800 Subject: [PATCH 9/9] fixed issue #45 --- .../Internal/ConsumerInvokerFactory.cs | 13 +++---- .../Internal/IConsumerInvoker.Default.cs | 36 ++++++++++--------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/DotNetCore.CAP/Internal/ConsumerInvokerFactory.cs b/src/DotNetCore.CAP/Internal/ConsumerInvokerFactory.cs index cdcb6d63e..1d2f5df0d 100644 --- a/src/DotNetCore.CAP/Internal/ConsumerInvokerFactory.cs +++ b/src/DotNetCore.CAP/Internal/ConsumerInvokerFactory.cs @@ -1,6 +1,5 @@ using System; using DotNetCore.CAP.Abstractions; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace DotNetCore.CAP.Internal @@ -23,15 +22,13 @@ public ConsumerInvokerFactory( public IConsumerInvoker CreateInvoker(ConsumerContext consumerContext) { - using (var scope = _serviceProvider.CreateScope()) + var context = new ConsumerInvokerContext(consumerContext) { - var context = new ConsumerInvokerContext(consumerContext) - { - Result = new DefaultConsumerInvoker(_logger, scope.ServiceProvider, _modelBinderFactory, consumerContext) - }; + Result = new DefaultConsumerInvoker(_logger, _serviceProvider, + _modelBinderFactory, consumerContext) + }; - return context.Result; - } + return context.Result; } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs b/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs index 41186266d..0f408beaa 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs @@ -35,25 +35,29 @@ public async Task InvokeAsync() { _logger.LogDebug("Executing consumer Topic: {0}", _consumerContext.ConsumerDescriptor.MethodInfo.Name); - var obj = ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, - _consumerContext.ConsumerDescriptor.ImplTypeInfo.AsType()); + using (var scope = _serviceProvider.CreateScope()) + { + var provider = scope.ServiceProvider; + var serviceType = _consumerContext.ConsumerDescriptor.ImplTypeInfo.AsType(); + var obj = ActivatorUtilities.GetServiceOrCreateInstance(provider, serviceType); - var jsonConent = _consumerContext.DeliverMessage.Content; - var message = Helper.FromJson(jsonConent); + var jsonConent = _consumerContext.DeliverMessage.Content; + var message = Helper.FromJson(jsonConent); - object result = null; - if (_executor.MethodParameters.Length > 0) - { - result = await ExecuteWithParameterAsync(obj, message.Content.ToString()); - } - else - { - result = await ExecuteAsync(obj); - } + object result = null; + if (_executor.MethodParameters.Length > 0) + { + result = await ExecuteWithParameterAsync(obj, message.Content.ToString()); + } + else + { + result = await ExecuteAsync(obj); + } - if (!string.IsNullOrEmpty(message.CallbackName)) - { - await SentCallbackMessage(message.Id, message.CallbackName, result); + if (!string.IsNullOrEmpty(message.CallbackName)) + { + await SentCallbackMessage(message.Id, message.CallbackName, result); + } } }