public async Task <ConsumerExecutedResult> InvokeAsync(ConsumerContext context) { _logger.LogDebug("Executing consumer Topic: {0}", context.ConsumerDescriptor.MethodInfo.Name); var executor = ObjectMethodExecutor.Create( context.ConsumerDescriptor.MethodInfo, context.ConsumerDescriptor.ImplTypeInfo); using (var scope = _serviceProvider.CreateScope()) { var provider = scope.ServiceProvider; var serviceType = context.ConsumerDescriptor.ImplTypeInfo.AsType(); var obj = ActivatorUtilities.GetServiceOrCreateInstance(provider, serviceType); var jsonContent = context.DeliverMessage.Content; var message = _messagePacker.UnPack(jsonContent); object resultObj; if (executor.MethodParameters.Length > 0) { resultObj = await ExecuteWithParameterAsync(executor, obj, message.Content); } else { resultObj = await ExecuteAsync(executor, obj); } return(new ConsumerExecutedResult(resultObj, message.Id, message.CallbackName)); } }
public async Task <ConsumerExecutedResult> InvokeAsync(ConsumerContext context, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); _logger.LogDebug("Executing consumer Topic: {0}", context.ConsumerDescriptor.MethodInfo.Name); var executor = ObjectMethodExecutor.Create( context.ConsumerDescriptor.MethodInfo, context.ConsumerDescriptor.ImplTypeInfo); using (var scope = _serviceProvider.CreateScope()) { var provider = scope.ServiceProvider; var srvType = context.ConsumerDescriptor.ServiceTypeInfo?.AsType(); var implType = context.ConsumerDescriptor.ImplTypeInfo.AsType(); object obj = null; if (srvType != null) { obj = provider.GetServices(srvType).FirstOrDefault(o => o.GetType() == implType); } if (obj == null) { obj = ActivatorUtilities.GetServiceOrCreateInstance(provider, implType); } var jsonContent = context.DeliverMessage.Content; var message = _messagePacker.UnPack(jsonContent); object resultObj; if (executor.MethodParameters.Length > 0) { resultObj = await ExecuteWithParameterAsync(executor, obj, message.Content); } else { resultObj = await ExecuteAsync(executor, obj); } return(new ConsumerExecutedResult(resultObj, message.Id, message.CallbackName)); } }