Пример #1
0
        public bool RegisterRoute(string pattern, string httpMethod, MethodInfo method, string routeGrainProviderPolicy)
        {
            if (this._routes.TryGetValue(pattern, out var grainRoutes))
            {
                if (grainRoutes.ContainsKey(httpMethod))
                {
                    return(false);
                }

                grainRoutes[httpMethod] = new GrainInvoker(this._serviceProvider, method, routeGrainProviderPolicy);
            }
            else
            {
                this._routes[pattern] = new Dictionary <string, GrainInvoker>(StringComparer.InvariantCultureIgnoreCase)
                {
                    [httpMethod] = new GrainInvoker(this._serviceProvider, method, routeGrainProviderPolicy)
                };
            }

            return(true);
        }
Пример #2
0
        public async Task Dispatch(HttpContext context)
        {
            var endpoint = (RouteEndpoint)context.GetEndpoint();
            var pattern  = endpoint.RoutePattern;
            // At this point we are sure we have a pattern and an invoker since a route was match for that particular endpoint
            var          allRoutes = this._routes[pattern.RawText];
            GrainInvoker invoker   = default;

            if (!allRoutes.TryGetValue("*", out invoker))
            {
                invoker = allRoutes[context.Request.Method];
            }

            IGrain grain = null;
            var    routeGrainProvider = invoker.RouteGrainProvider;

            try
            {
                grain = await routeGrainProvider.GetGrain(invoker.GrainType);
            }
            catch (Exception ex)
            {
                this._logger.LogError(ex, "");
            }

            if (grain == null)
            {
                //Check if status is set to OK and change to internal server error, the invoker's RouteGrainProvider implementation may handle this otherwise
                if (context.Response.StatusCode == (int)System.Net.HttpStatusCode.OK)
                {
                    this._logger.LogError($"Failure getting grain '{invoker.GrainType.FullName}' for route '{pattern.RawText}' with RouteGrainProvider '{routeGrainProvider.GetType()}' and was unhandled");
                    context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                }
                return;
            }

            await invoker.Invoke(grain, context);
        }