/// <summary>
        /// Create action from controller data.
        /// <para></para>
        /// All controller logic (like creating controllers, ModelBinders etc.) must be here.
        /// To know why - read readme.txt in Telegram.Bot.AspNetPipeline.Mvc.Controllers folder.
        /// <para></para>
        /// Current implemention use only <see cref="ControllerActionContext"/>, but you can use passed parameters
        /// to generate some predefined code to make it faster.
        /// </summary>
        public RouteActionDelegate CreateAction(Type controllerType, MethodInfo methodInfo, RouteInfo routeInfo)
        {
            if (!typeof(Task).IsAssignableFrom(methodInfo.ReturnType))
            {
                throw new TelegramAspException($"Exception with '{methodInfo}' in '{controllerType.Name}'. " +
                                               $"Controller method must return Task.");
            }

            RouteActionDelegate routeActionDelegate = async(actionContext) =>
            {
#if DEBUG
                if (actionContext is ControllerActionContext controllerActionContext)
                {
                    if (controllerActionContext.ActionDescriptor.MethodInfo != methodInfo)
                    {
                        throw new TelegramAspException("Predefined MethodInfo and context MethodInfo is different.");
                    }
                }
#endif
                if (!(actionContext is ControllerActionContext))
                {
                    throw new TelegramAspException(
                              $"Controller actions can be invoked only with {typeof(ControllerActionContext).Name}.\n" +
                              $"Please, contact library author if exception throwed in default pipeline."
                              );
                }
                await Handler((ControllerActionContext)actionContext);
            };

            return(routeActionDelegate);
        }
Exemple #2
0
        public static void MapRouteAction(
            this IUseMvcBuilder @this,
            RouteActionDelegate routeAction,
            string template          = null,
            int order                = 0,
            string name              = null,
            UpdateType[] updateTypes = null
            )
        {
            var routeInfo = new RouteInfo(template, order, name, updateTypes);

            @this.MapRouteAction(routeAction, routeInfo);
        }
        /// <summary>
        /// Just like you do with controller methods, but for delegates.
        /// </summary>
        public void MapRouteAction(RouteActionDelegate routeAction, RouteInfo routeInfo)
        {
            if (routeAction == null)
            {
                throw new ArgumentNullException(nameof(routeAction));
            }
            if (routeInfo == null)
            {
                throw new ArgumentNullException(nameof(routeInfo));
            }
            var tuple = new Tuple <RouteActionDelegate, RouteInfo>(routeAction, routeInfo);

            _routes.Add(new ActionDescriptor(routeAction, routeInfo));
        }
 public ControllerActionDescriptor(
     RouteActionDelegate handler,
     RouteInfo routeInfo,
     MethodInfo methodInfo,
     Type controllerType
     ) : base(handler, routeInfo)
 {
     MethodInfo     = methodInfo;
     ControllerType = controllerType;
     Parameters     = methodInfo.GetParameters();
     foreach (var param in Parameters)
     {
         _parametersByName[param.Name] = param;
     }
 }
Exemple #5
0
 public ActionDescriptor(RouteActionDelegate handler, RouteInfo routeInfo)
 {
     RouteInfo = routeInfo;
     Handler   = handler;
 }