Exemple #1
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);
            }
        }