Exemplo n.º 1
0
        private async Task <object> ExecuteWithParameterAsync(ObjectMethodExecutor executor, object @class, object[] parameter)
        {
            if (executor.IsMethodAsync)
            {
                return(await executor.ExecuteAsync(@class, parameter));
            }

            return(executor.Execute(@class, parameter));
        }
Exemplo n.º 2
0
        private async Task <object> ExecuteAsync(ObjectMethodExecutor executor, object @class)
        {
            if (executor.IsMethodAsync)
            {
                return(await executor.ExecuteAsync(@class));
            }

            return(executor.Execute(@class));
        }
Exemplo n.º 3
0
        public async Task <ConsumerExecutedResult> InvokeAsync(ConsumerContext context,
                                                               CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var methodInfo = context.ConsumerDescriptor.MethodInfo;

            _logger.LogDebug("Executing subscriber method : {0}", methodInfo.Name);

            var executor = _executors.GetOrAdd(methodInfo.MetadataToken,
                                               x => ObjectMethodExecutor.Create(methodInfo, context.ConsumerDescriptor.ImplTypeInfo));

            using var scope = _serviceProvider.CreateScope();

            var provider = scope.ServiceProvider;

            var obj = GetInstance(provider, context);

            var message = context.DeliverMessage;
            var parameterDescriptors = context.ConsumerDescriptor.Parameters;
            var executeParameters    = new object[parameterDescriptors.Count];

            for (var i = 0; i < parameterDescriptors.Count; i++)
            {
                if (parameterDescriptors[i].IsFromCap)
                {
                    executeParameters[i] = new CapHeader(message.Headers);
                }
                else
                {
                    if (message.Value != null)
                    {
                        if (message.Value is JToken jToken) //reading from storage
                        {
                            executeParameters[i] = jToken.ToObject(parameterDescriptors[i].ParameterType);
                        }
                        else
                        {
                            var converter = TypeDescriptor.GetConverter(parameterDescriptors[i].ParameterType);
                            if (converter.CanConvertFrom(message.Value.GetType()))
                            {
                                executeParameters[i] = converter.ConvertFrom(message.Value);
                            }
                            else
                            {
                                executeParameters[i] =
                                    Convert.ChangeType(message.Value, parameterDescriptors[i].ParameterType);
                            }
                        }
                    }
                }
            }

            var resultObj = await ExecuteWithParameterAsync(executor, obj, executeParameters);

            return(new ConsumerExecutedResult(resultObj, message.GetId(), message.GetCallbackName()));
        }
Exemplo n.º 4
0
        public async Task <ConsumerExecutedResult> InvokeAsync(ConsumerContext context, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var methodInfo = context.ConsumerDescriptor.MethodInfo;

            _logger.LogDebug("Executing subscriber method : {0}", methodInfo.Name);

            var executor = _executors.GetOrAdd(methodInfo.MetadataToken, x => ObjectMethodExecutor.Create(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 message = context.DeliverMessage;
                var parameterDescriptors = context.ConsumerDescriptor.Parameters;
                var executeParameters    = new object[parameterDescriptors.Count];
                for (var i = 0; i < parameterDescriptors.Count; i++)
                {
                    if (parameterDescriptors[i].IsFromCap)
                    {
                        executeParameters[i] = new CapHeader(message.Headers);
                    }
                    else
                    {
                        if (message.Value is JToken jToken)  //reading from storage
                        {
                            executeParameters[i] = jToken.ToObject(parameterDescriptors[i].ParameterType);
                        }
                        else
                        {
                            executeParameters[i] = message.Value;
                        }
                    }
                }

                var resultObj = await ExecuteWithParameterAsync(executor, obj, executeParameters);

                return(new ConsumerExecutedResult(resultObj, message.GetId(), message.GetCallbackName()));
            }
        }
Exemplo n.º 5
0
        public DefaultConsumerInvoker(ILogger logger,
                                      IServiceProvider serviceProvider,
                                      IModelBinderFactory modelBinderFactory,
                                      ConsumerContext consumerContext)
        {
            _modelBinderFactory = modelBinderFactory;
            _serviceProvider    = serviceProvider;
            _logger             = logger;
            _consumerContext    = consumerContext;

            _executor = ObjectMethodExecutor.Create(_consumerContext.ConsumerDescriptor.MethodInfo,
                                                    _consumerContext.ConsumerDescriptor.ImplTypeInfo);
        }
Exemplo n.º 6
0
        public DefaultConsumerInvoker(ILogger logger,
                                      IServiceProvider serviceProvider,
                                      IModelBinderFactory modelBinderFactory,
                                      ConsumerContext consumerContext)
        {
            _modelBinderFactory = modelBinderFactory;
            _serviceProvider    = serviceProvider;
            _logger             = logger ?? throw new ArgumentNullException(nameof(logger));

            _consumerContext = consumerContext ?? throw new ArgumentNullException(nameof(consumerContext));
            _executor        = ObjectMethodExecutor.Create(_consumerContext.ConsumerDescriptor.MethodInfo,
                                                           _consumerContext.ConsumerDescriptor.ImplTypeInfo);
        }
Exemplo n.º 7
0
        private async Task <object> ExecuteWithParameterAsync(ObjectMethodExecutor executor, object @class,
                                                              object[] parameter)
        {
            if (executor.IsMethodAsync)
            {
                var response = await Helper.TimeOutExecuteAsync(_options.ConsumerMaxTimeOut,
                                                                async (cts) => await executor.ExecuteAsync(@class, parameter));

                return(response);
            }
            else
            {
                var response = Helper.TimeOutExecute(_options.ConsumerMaxTimeOut,
                                                     (cts) => executor.Execute(@class, parameter));
                return(response);
            }
        }
Exemplo n.º 8
0
        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));
            }
        }
Exemplo n.º 9
0
        public async Task<ConsumerExecutedResult> InvokeAsync(ConsumerContext context, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var methodInfo = context.ConsumerDescriptor.MethodInfo;
            var reflectedTypeHandle = methodInfo.ReflectedType.TypeHandle.Value;
            var methodHandle = methodInfo.MethodHandle.Value;
            var key = $"{reflectedTypeHandle}_{methodHandle}";

            var executor = _executors.GetOrAdd(key, x => ObjectMethodExecutor.Create(methodInfo, context.ConsumerDescriptor.ImplTypeInfo));

            using var scope = _serviceProvider.CreateScope();

            var provider = scope.ServiceProvider;

            var obj = GetInstance(provider, context);

            var message = context.DeliverMessage;
            var parameterDescriptors = context.ConsumerDescriptor.Parameters;
            var executeParameters = new object[parameterDescriptors.Count];
            for (var i = 0; i < parameterDescriptors.Count; i++)
            {
                var parameterDescriptor = parameterDescriptors[i];
                if (parameterDescriptor.IsFromCap)
                {
                    executeParameters[i] = GetCapProvidedParameter(parameterDescriptor, message, cancellationToken);
                }
                else
                {
                    if (message.Value != null)
                    {
                        if (_serializer.IsJsonType(message.Value))  // use ISerializer when reading from storage, skip other objects if not Json
                        {
                            executeParameters[i] = _serializer.Deserialize(message.Value, parameterDescriptor.ParameterType);
                        }
                        else
                        {
                            var converter = TypeDescriptor.GetConverter(parameterDescriptor.ParameterType);
                            if (converter.CanConvertFrom(message.Value.GetType()))
                            {
                                executeParameters[i] = converter.ConvertFrom(message.Value);
                            }
                            else
                            {
                                if (parameterDescriptor.ParameterType.IsInstanceOfType(message.Value))
                                {
                                    executeParameters[i] = message.Value;
                                }
                                else
                                {
                                    executeParameters[i] = Convert.ChangeType(message.Value, parameterDescriptor.ParameterType);
                                }
                            }
                        }
                    }
                }
            }

            var filter = provider.GetService<ISubscribeFilter>();
            object resultObj = null;
            try
            {
                if (filter != null)
                {
                    var etContext = new ExecutingContext(context, executeParameters);
                    filter.OnSubscribeExecuting(etContext);
                    executeParameters = etContext.Arguments;
                }

                resultObj = await ExecuteWithParameterAsync(executor, obj, executeParameters);

                if (filter != null)
                {
                    var edContext = new ExecutedContext(context, resultObj);
                    filter.OnSubscribeExecuted(edContext);
                    resultObj = edContext.Result;
                }
            }
            catch (Exception e)
            {
                if (filter != null)
                {
                    var exContext = new ExceptionContext(context, e);
                    filter.OnSubscribeException(exContext);
                    if (!exContext.ExceptionHandled)
                    {
                        throw exContext.Exception;
                    }

                    if (exContext.Result != null)
                    {
                        resultObj = exContext.Result;
                    }
                }
                else
                {
                    throw;
                }
            }

            return new ConsumerExecutedResult(resultObj, message.GetId(), message.GetCallbackName());
        }
Exemplo n.º 10
0
        public async Task <ConsumerExecutedResult> InvokeAsync(ConsumerContext context, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var methodInfo    = context.ConsumerDescriptor.MethodInfo;
            var reflectedType = methodInfo.ReflectedType.Name;

            _logger.LogDebug("Executing subscriber method : {0}", methodInfo.Name);

            var key      = $"{methodInfo.Module.Name}_{reflectedType}_{methodInfo.MetadataToken}";
            var executor = _executors.GetOrAdd(key, x => ObjectMethodExecutor.Create(methodInfo, context.ConsumerDescriptor.ImplTypeInfo));

            using (var scope = _serviceProvider.GetService <ICapServiceScope>())
            {
                var obj = scope.GetInstance(context);

                var message = context.DeliverMessage;
                var parameterDescriptors = context.ConsumerDescriptor.Parameters;
                var executeParameters    = new object[parameterDescriptors.Count];
                for (var i = 0; i < parameterDescriptors.Count; i++)
                {
                    if (parameterDescriptors[i].IsFromCap)
                    {
                        executeParameters[i] = new CapHeader(message.Headers);
                    }
                    else
                    {
                        if (message.Value != null)
                        {
                            if (_serializer.IsJsonType(message.Value))  // use ISerializer when reading from storage, skip other objects if not Json
                            {
                                executeParameters[i] = _serializer.Deserialize(message.Value, parameterDescriptors[i].ParameterType);
                            }
                            else
                            {
                                var converter = TypeDescriptor.GetConverter(parameterDescriptors[i].ParameterType);
                                if (converter.CanConvertFrom(message.Value.GetType()))
                                {
                                    executeParameters[i] = converter.ConvertFrom(message.Value);
                                }
                                else
                                {
                                    if (parameterDescriptors[i].ParameterType.IsInstanceOfType(message.Value))
                                    {
                                        executeParameters[i] = message.Value;
                                    }
                                    else
                                    {
                                        executeParameters[i] = Convert.ChangeType(message.Value, parameterDescriptors[i].ParameterType);
                                    }
                                }
                            }
                        }
                    }
                }

                var resultObj = await ExecuteWithParameterAsync(executor, obj, executeParameters);

                return(new ConsumerExecutedResult(resultObj, message.GetId(), message.GetCallbackName()));
            }
        }
Exemplo n.º 11
0
        //public bool IsTypeAssignableFromIActionResult { get; }

        public static ObjectMethodExecutor Create(MethodInfo methodInfo, TypeInfo targetTypeInfo)
        {
            var executor = new ObjectMethodExecutor(methodInfo, targetTypeInfo);

            return(executor);
        }