Exemple #1
0
        private async Task <IViewComponentResult> InvokeAsyncCore(ViewComponentContext context)
        {
            var component = _viewComponentFactory.CreateViewComponent(context);

            using (_logger.ViewComponentScope(context))
            {
                var method    = context.ViewComponentDescriptor.MethodInfo;
                var arguments = ControllerActionExecutor.PrepareArguments(context.Arguments, method.GetParameters());

                var methodExecutor = _viewComponentInvokerCache.GetViewComponentMethodExecutor(context);

                _diagnosticSource.BeforeViewComponent(context, component);
                _logger.ViewComponentExecuting(context, arguments);

                var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0;
                var result         = await ControllerActionExecutor.ExecuteAsync(methodExecutor, component, arguments);

                var viewComponentResult = CoerceToViewComponentResult(result);
                _logger.ViewComponentExecuted(context, startTimestamp, viewComponentResult);
                _diagnosticSource.AfterViewComponent(context, viewComponentResult, component);

                _viewComponentFactory.ReleaseViewComponent(context, component);

                return(viewComponentResult);
            }
        }
Exemple #2
0
        private async Task <IViewComponentResult> InvokeAsyncCore(ObjectMethodExecutor executor, ViewComponentContext context)
        {
            var component = _viewComponentFactory.CreateViewComponent(context);

            using (_logger.ViewComponentScope(context))
            {
                var arguments = PrepareArguments(context.Arguments, executor);

                _diagnosticListener.BeforeViewComponent(context, component);
                _logger.ViewComponentExecuting(context, arguments);

                var stopwatch = ValueStopwatch.StartNew();

                object resultAsObject;
                var    returnType = executor.MethodReturnType;

                if (returnType == typeof(Task <IViewComponentResult>))
                {
                    var task = executor.Execute(component, arguments);
                    if (task is null)
                    {
                        throw new InvalidOperationException(Resources.ViewComponent_MustReturnValue);
                    }

                    resultAsObject = await(Task <IViewComponentResult>) task;
                }
                else if (returnType == typeof(Task <string>))
                {
                    var task = executor.Execute(component, arguments);
                    if (task is null)
                    {
                        throw new InvalidOperationException(Resources.ViewComponent_MustReturnValue);
                    }

                    resultAsObject = await(Task <string>) task;
                }
                else if (returnType == typeof(Task <IHtmlContent>))
                {
                    var task = executor.Execute(component, arguments);
                    if (task is null)
                    {
                        throw new InvalidOperationException(Resources.ViewComponent_MustReturnValue);
                    }

                    resultAsObject = await(Task <IHtmlContent>) task;
                }
                else
                {
                    resultAsObject = await executor.ExecuteAsync(component, arguments);
                }

                var viewComponentResult = CoerceToViewComponentResult(resultAsObject);
                _logger.ViewComponentExecuted(context, stopwatch.GetElapsedTime(), viewComponentResult);
                _diagnosticListener.AfterViewComponent(context, viewComponentResult, component);

                _viewComponentFactory.ReleaseViewComponent(context, component);

                return(viewComponentResult);
            }
        }
        private async Task <IViewComponentResult> InvokeAsyncCore(ObjectMethodExecutor executor, ViewComponentContext context)
        {
            var component = _viewComponentFactory.CreateViewComponent(context);

            using (_logger.ViewComponentScope(context))
            {
                var arguments = PrepareArguments(context.Arguments, executor);

                _diagnosticSource.BeforeViewComponent(context, component);
                _logger.ViewComponentExecuting(context, arguments);

                var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0;

                object resultAsObject;
                var    returnType = executor.MethodReturnType;

                if (returnType == typeof(Task <IViewComponentResult>))
                {
                    resultAsObject = await(Task <IViewComponentResult>) executor.Execute(component, arguments);
                }
                else if (returnType == typeof(Task <string>))
                {
                    resultAsObject = await(Task <string>) executor.Execute(component, arguments);
                }
                else if (returnType == typeof(Task <IHtmlContent>))
                {
                    resultAsObject = await(Task <IHtmlContent>) executor.Execute(component, arguments);
                }
                else
                {
                    resultAsObject = await executor.ExecuteAsync(component, arguments);
                }

                var viewComponentResult = CoerceToViewComponentResult(resultAsObject);
                _logger.ViewComponentExecuted(context, startTimestamp, viewComponentResult);
                _diagnosticSource.AfterViewComponent(context, viewComponentResult, component);

                _viewComponentFactory.ReleaseViewComponent(context, component);

                return(viewComponentResult);
            }
        }
        public Task InvokeAsync(ViewComponentContext context)
        {
            var uncastedcomponent = _viewComponentFactory.CreateViewComponent(context);

            Debug.Assert(uncastedcomponent is ScriptedViewComponent);
            var component = (ScriptedViewComponent)uncastedcomponent;
            var obj       = component.Invoke(context.Arguments);

            _viewComponentFactory.ReleaseViewComponent(context, component);

            var result = CoerceToViewComponentResult(obj);

            return(result.ExecuteAsync(context));
        }
Exemple #5
0
        public async Task InvokeAsync(ViewComponentContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var executor = _viewComponentInvokerCache.GetViewComponentMethodExecutor(context);

            var returnType = executor.MethodReturnType;

            if (returnType == typeof(void) || returnType == typeof(Task))
            {
                throw new InvalidOperationException(Resources.ViewComponent_MustReturnValue);
            }

            IViewComponentResult result;
            object?component = null;

            try
            {
                component = _viewComponentFactory.CreateViewComponent(context);
                if (executor.IsMethodAsync)
                {
                    result = await InvokeAsyncCore(executor, component, context);
                }
                else
                {
                    // We support falling back to synchronous if there is no InvokeAsync method, in this case we'll still
                    // execute the IViewResult asynchronously.
                    result = InvokeSyncCore(executor, component, context);
                }
            }
            finally
            {
                if (component != null)
                {
                    await _viewComponentFactory.ReleaseViewComponentAsync(context, executor);
                }
            }

            await result.ExecuteAsync(context);
        }