public Task InvokeAsync()
        {
            using (_logger.BeginScope("consumer invoker begin"))
            {
                _logger.LogDebug("Executing consumer Topic: {0}", _consumerContext.ConsumerDescriptor.MethodInfo.Name);

                var obj = ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider,
                                                                        _consumerContext.ConsumerDescriptor.ImplTypeInfo.AsType());

                var value = _consumerContext.DeliverMessage.Content;

                if (_executor.MethodParameters.Length > 0)
                {
                    var firstParameter = _executor.MethodParameters[0];

                    var bindingContext = ModelBindingContext.CreateBindingContext(value,
                                                                                  firstParameter.Name, firstParameter.ParameterType);

                    _modelBinder.BindModelAsync(bindingContext);
                    _executor.Execute(obj, bindingContext.Result);
                }
                else
                {
                    _executor.Execute(obj);
                }
                return(Task.CompletedTask);
            }
        }
        public async Task <ModelBindingResult> BindModelAsync(
            [NotNull] ParameterDescriptor parameter,
            [NotNull] ModelStateDictionary modelState,
            [NotNull] OperationBindingContext operationContext)
        {
            var metadata            = _modelMetadataProvider.GetMetadataForType(parameter.ParameterType);
            var modelBindingContext = ModelBindingContext.CreateBindingContext(
                operationContext,
                modelState,
                metadata,
                parameter.BindingInfo,
                parameter.Name);

            var modelBindingResult = await operationContext.ModelBinder.BindModelAsync(modelBindingContext);

            if (modelBindingResult.IsModelSet)
            {
                _validator.Validate(
                    operationContext.ValidatorProvider,
                    modelState,
                    modelBindingContext.ValidationState,
                    modelBindingResult.Key,
                    modelBindingResult.Model);
            }

            return(modelBindingResult);
        }
        public async Task <ModelBindingResult> BindModelAsync(
            ParameterDescriptor parameter,
            OperationBindingContext operationContext)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (operationContext == null)
            {
                throw new ArgumentNullException(nameof(operationContext));
            }

            var metadata            = _modelMetadataProvider.GetMetadataForType(parameter.ParameterType);
            var modelBindingContext = ModelBindingContext.CreateBindingContext(
                operationContext,
                metadata,
                parameter.BindingInfo,
                parameter.Name);

            var modelBindingResult = await operationContext.ModelBinder.BindModelAsync(modelBindingContext);

            if (modelBindingResult.IsModelSet)
            {
                _validator.Validate(
                    operationContext.ActionContext,
                    operationContext.ValidatorProvider,
                    modelBindingContext.ValidationState,
                    modelBindingResult.Key,
                    modelBindingResult.Model);
            }

            return(modelBindingResult);
        }
Exemple #4
0
        /// <inheritdocs />
        public async Task <IDictionary <string, object> > BindArgumentsAsync(WidgetContext context, MethodInfo method, IDictionary <string, object> values)
        {
            var bindingContext = new OperationBindingContext
            {
                HttpContext       = context.ViewContext.HttpContext,
                InputFormatters   = _options.InputFormatters,
                MetadataProvider  = _modelMetadataProvider,
                ModelBinder       = new CompositeModelBinder(_options.ModelBinders),
                ValidatorProvider = new CompositeModelValidatorProvider(_options.ModelValidatorProviders),
                ValueProvider     = await CompositeValueProvider.CreateAsync(_options.ValueProviderFactories, new ValueProviderFactoryContext(context.ViewContext.HttpContext, context.ViewContext.RouteData.Values))
            };

            var arguments  = new Dictionary <string, object>(StringComparer.Ordinal);
            var parameters = method.GetParameters();

            foreach (var parameter in parameters)
            {
                if (values.ContainsKey(parameter.Name))
                {
                    arguments.Add(parameter.Name, values[parameter.Name]);
                }
                else
                {
                    var attribute   = parameter.GetCustomAttributes().OfType <IBindingSourceMetadata>().FirstOrDefault();
                    var bindingInfo = new BindingInfo()
                    {
                        BindingSource = attribute?.BindingSource
                    };

                    var metadata            = _modelMetadataProvider.GetMetadataForType(parameter.ParameterType);
                    var modelBindingContext = ModelBindingContext.CreateBindingContext(
                        bindingContext,
                        context.ModelState,
                        metadata,
                        bindingInfo,
                        parameter.Name);

                    var result = await bindingContext.ModelBinder.BindModelAsync(modelBindingContext);

                    if (result.IsModelSet)
                    {
                        _objectModelValidator.Validate(bindingContext.ValidatorProvider, context.ModelState, modelBindingContext.ValidationState, result.Key, result.Model);
                        arguments.Add(parameter.Name, result.Model);
                    }
                    else
                    {
                        arguments.Add(parameter.Name, Activator.CreateInstance(parameter.ParameterType));
                    }
                }
            }

            return(arguments);
        }