@@ -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; | |||
} | |||
} | |||
} |
@@ -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<Message>(jsonConent); | |||
var jsonConent = _consumerContext.DeliverMessage.Content; | |||
var message = Helper.FromJson<Message>(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); | |||
} | |||
} | |||
} | |||
@@ -48,19 +48,23 @@ namespace DotNetCore.CAP.Internal | |||
IServiceProvider provider) | |||
{ | |||
var executorDescriptorList = new List<ConsumerExecutorDescriptor>(); | |||
var consumerServices = provider.GetServices<ICapSubscribe>(); | |||
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<ICapSubscribe>(); | |||
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<ConsumerExecutorDescriptor> FindConsumersFromControllerTypes( | |||
@@ -76,7 +80,7 @@ namespace DotNetCore.CAP.Internal | |||
{ | |||
executorDescriptorList.AddRange(GetTopicAttributesDescription(typeInfo)); | |||
} | |||
} | |||
} | |||
return executorDescriptorList; | |||
} | |||