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);
            }
        }
        /// <summary>
        /// Resolves the value the current selection node.
        /// </summary>
        /// <param name="context">The <see cref="T:Dora.GraphQL.GraphTypes.ResolverContext" /> in which the field value is resoved.</param>
        /// <returns>
        /// The resolved field's value.
        /// </returns>
        public async ValueTask <object> ResolveAsync(ResolverContext context)
        {
            var arguments = new object[_operation.Parameters.Count];
            var index     = 0;

            foreach (var parameter in _operation.Parameters.Values)
            {
                var bindingContext = new ArgumentBinderContext(parameter, context);
                var result         = await _binder.BindAsync(bindingContext);

                var value = result.IsArgumentBound
                    ? result.Value
                    : parameter.ParameterInfo.DefaultValue;
                arguments[index++] = value;
            }
            var serviceProvider = context.GraphContext.RequestServices;
            var service         = ActivatorUtilities.CreateInstance(serviceProvider, _operation.Service.ServiceType);

            var returnType = _executor.MethodInfo.ReturnType;

            if (typeof(Task).IsAssignableFrom(returnType) || (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(ValueTask <>)))
            {
                return(await _executor.ExecuteAsync(service, arguments));
            }
            return(_executor.Execute(service, arguments));
        }
        private IViewComponentResult InvokeSyncCore(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 result;

                try
                {
                    result = executor.Execute(component, arguments);
                }
                finally
                {
                    _viewComponentFactory.ReleaseViewComponent(context, component);
                }

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

                _viewComponentFactory.ReleaseViewComponent(context, component);

                return(viewComponentResult);
            }
        }
Exemple #4
0
        private IViewComponentResult InvokeSyncCore(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    stopwatch = ValueStopwatch.StartNew();
                object result;

                try
                {
                    result = executor.Execute(component, arguments);
                }
                finally
                {
                    _viewComponentFactory.ReleaseViewComponent(context, component);
                }

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

                _viewComponentFactory.ReleaseViewComponent(context, component);

                return(viewComponentResult);
            }
        }
        private async Task <object> ExecuteWithParameterAsync(ObjectMethodExecutor executor,
                                                              object @class, string parameterString)
        {
            var firstParameter = executor.MethodParameters[0];

            try
            {
                var binder     = _modelBinderFactory.CreateBinder(firstParameter);
                var bindResult = await binder.BindModelAsync(parameterString);

                if (bindResult.IsSuccess)
                {
                    if (executor.IsMethodAsync)
                    {
                        return(await executor.ExecuteAsync(@class, bindResult.Model));
                    }

                    return(executor.Execute(@class, bindResult.Model));
                }

                throw new MethodBindException(
                          $"Parameters:{firstParameter.Name} bind failed! ParameterString is: {parameterString} ");
            }
            catch (FormatException ex)
            {
                _logger.ModelBinderFormattingException(executor.MethodInfo?.Name, firstParameter.Name, parameterString,
                                                       ex);
                return(null);
            }
        }
 public override async ValueTask <IActionResult> Execute(
     IActionResultTypeMapper mapper,
     ObjectMethodExecutor executor,
     object controller,
     object[] arguments)
 {
     await(Task) executor.Execute(controller, arguments);
     return(new EmptyResult());
 }
        private async Task <object> ExecuteAsync(ObjectMethodExecutor executor, object @class)
        {
            if (executor.IsMethodAsync)
            {
                return(await executor.ExecuteAsync(@class));
            }

            return(executor.Execute(@class));
        }
        private static async Task <object> ExecuteWithParameterAsync(ObjectMethodExecutor executor, object @class, object[] parameter)
        {
            if (executor.IsMethodAsync)
            {
                return(await executor.ExecuteAsync(@class, parameter));
            }

            return(executor.Execute(@class, parameter));
        }
 public override ValueTask <IActionResult> Execute(
     IActionResultTypeMapper mapper,
     ObjectMethodExecutor executor,
     object controller,
     object?[]?arguments)
 {
     executor.Execute(controller, arguments);
     return(new ValueTask <IActionResult>(new EmptyResult()));
 }
 public override ValueTask <IActionResult> Execute(
     ActionContext actionContext,
     IActionResultTypeMapper mapper,
     ObjectMethodExecutor executor,
     object controller,
     object?[]?arguments)
 {
     executor.Execute(controller, arguments);
     return(new(EmptyResultInstance));
 }
 private async ValueTask <object> ExecuteMethod(ObjectMethodExecutor methodExecutor, Hub hub, object[] arguments)
 {
     if (methodExecutor.IsMethodAsync)
     {
         if (methodExecutor.MethodReturnType == typeof(Task))
         {
             await(Task) methodExecutor.Execute(hub, arguments);
             return(null);
         }
         else
         {
             return(await methodExecutor.ExecuteAsync(hub, arguments));
         }
     }
     else
     {
         return(methodExecutor.Execute(hub, arguments));
     }
 }
        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);
            }
        }
        private async Task <IWebSocketActionResult> InvokeActionMethodAsync(ObjectMethodExecutor executor, object controller, IDictionary <string, object> parameters)
        {
            // Order the parameters used to invoke the action
            var orderedParameters = ControllerActionExecutor.PrepareArguments(parameters, executor);

            // Execute the action
            var resultAsObject = executor.Execute(controller, orderedParameters);

            // If the result is not of type IWebSocketActionResult, create a WebSocketObjectResult with the result
            return(resultAsObject as IWebSocketActionResult ?? new WebSocketObjectResult(resultAsObject));
        }
Exemple #14
0
        private static async Task <object> ExecuteHubMethod(ObjectMethodExecutor methodExecutor, THub hub, object[] arguments)
        {
            // ReadableChannel is awaitable but we don't want to await it.
            if (methodExecutor.IsMethodAsync && !IsChannel(methodExecutor.MethodReturnType, out _))
            {
                if (methodExecutor.MethodReturnType == typeof(Task))
                {
                    await(Task) methodExecutor.Execute(hub, arguments);
                }
                else
                {
                    return(await methodExecutor.ExecuteAsync(hub, arguments));
                }
            }
            else
            {
                return(methodExecutor.Execute(hub, arguments));
            }

            return(null);
        }
        public override ValueTask <IActionResult> Execute(
            IActionResultTypeMapper mapper,
            ObjectMethodExecutor executor,
            object controller,
            object?[]?arguments)
        {
            // Sync method returning arbitrary object
            var returnValue  = executor.Execute(controller, arguments);
            var actionResult = ConvertToActionResult(mapper, returnValue, executor.MethodReturnType);

            return(new ValueTask <IActionResult>(actionResult));
        }
        public override ValueTask <IActionResult> Execute(
            IActionResultTypeMapper mapper,
            ObjectMethodExecutor executor,
            object controller,
            object?[]?arguments)
        {
            var actionResult = (IActionResult)executor.Execute(controller, arguments) !;

            EnsureActionResultNotNull(executor, actionResult);

            return(new ValueTask <IActionResult>(actionResult));
        }
        public override async ValueTask <IActionResult> Execute(
            IActionResultTypeMapper mapper,
            ObjectMethodExecutor executor,
            object controller,
            object?[]?arguments)
        {
            // Async method returning Task<IActionResult>
            // Avoid extra allocations by calling Execute rather than ExecuteAsync and casting to Task<IActionResult>.
            var returnValue  = executor.Execute(controller, arguments);
            var actionResult = await(Task <IActionResult>) returnValue !;

            EnsureActionResultNotNull(executor, actionResult);

            return(actionResult);
        }
Exemple #18
0
    private IViewComponentResult InvokeSyncCore(ObjectMethodExecutor executor, object component, ViewComponentContext context)
    {
        using (Log.ViewComponentScope(_logger, context))
        {
            var arguments = PrepareArguments(context.Arguments, executor);

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

            var    stopwatch = ValueStopwatch.StartNew();
            object?result;

            result = executor.Execute(component, arguments);

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

            return(viewComponentResult);
        }
    }
 public Task RouteAsync(RouteContext route)
 {
     if (route.Handler == null)
     {
         route.Handler = async(context) =>
         {
             object   instance   = context.RequestServices.GetRequiredService(this.Service);
             object[] methodArgs = context.Request.Parameters;
             object   resultObj;
             if (_methodExecutor.IsMethodAsync)
             {
                 resultObj = await _methodExecutor.ExecuteAsync(instance, methodArgs);
             }
             else
             {
                 resultObj = _methodExecutor.Execute(instance, methodArgs);
             }
             context.Response.Success(resultObj);
         };
     }
     return(Task.CompletedTask);
 }
Exemple #20
0
 public object Compiled() => _compiled.Execute(this, _parameters);
Exemple #21
0
 public object Compiled() => ((Task <string>)_compiled.Execute(this, _parameters)).Result;