From 53f72d8ec4242d7302bd59a5f8a8d0cc9ad6d209 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 12 Sep 2017 21:08:50 +0800 Subject: [PATCH] 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 cdcb6d6..1d2f5df 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 @@ namespace DotNetCore.CAP.Internal 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 4118626..0f408be 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs @@ -35,25 +35,29 @@ namespace DotNetCore.CAP.Internal { _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); + } } }