Ejemplo n.º 1
0
        public object Execute(IRequest req, bool applyFilters)
        {
            try
            {
                req.SetInProcessRequest();

                var restPath = req is IHttpRequest httpReq
                    ? RestHandler.FindMatchingRestPath(httpReq, out _)
                    : RestHandler.FindMatchingRestPath(req.Verb, req.PathInfo, out _);

                //req.SetRoute(restPath as RestPath);
                //req.OperationName = restPath.RequestType.GetOperationName();
                var requestDto = RestHandler.CreateRequestAsync(req, restPath).Result;
                //req.Dto = requestDto;

                if (applyFilters)
                {
                    //requestDto = appHost.ApplyRequestConvertersAsync(req, requestDto).Result;

                    //appHost.ApplyRequestFiltersAsync(req, req.Response, requestDto).Wait();
                    //if (req.Response.IsClosed)
                    //    return null;
                }

                var response = Execute(requestDto, req);

                return(applyFilters
                    ? ApplyResponseFiltersAsync(response, req).Result
                    : response);
            }
            finally
            {
                req.ReleaseIfInProcessRequest();
            }
        }
Ejemplo n.º 2
0
        public object Execute(IRequest req)
        {
            string contentType;
            var    restPath = RestHandler.FindMatchingRestPath(req.Verb, req.PathInfo, out contentType);

            req.SetRoute(restPath as RestPath);
            req.OperationName = restPath.RequestType.GetOperationName();
            var request = RestHandler.CreateRequest(req, restPath);

            req.Dto = request;

            if (appHost.ApplyRequestFilters(req, req.Response, request))
            {
                return(null);
            }

            var response = Execute(request, req);

            if (appHost.ApplyResponseFilters(req, req.Response, response))
            {
                return(null);
            }

            return(response);
        }
        public void RegisterService(Type serviceType, ITypeFactory serviceFactoryFn)
        {
            if (!Service.IsServiceType(serviceType))
            {
                throw new ArgumentException($"{serviceType.FullName} is not a service type that implements IService.");
            }

            if (!serviceActionMap.TryGetValue(serviceType, out Dictionary <Type, List <ActionContext> > actionMap))
            {
                var serviceExecDef = typeof(ServiceExec <>).MakeGenericType(serviceType);
                var iserviceExec   = (IServiceExec)serviceExecDef.CreateInstance();
                iserviceExec.Reset(AppHost);
                actionMap = iserviceExec.ActionMap;
                foreach (var requestType in actionMap.Keys)
                {
                    ServiceExecFn handlerFn = (req, dto) =>
                    {
                        var service = (serviceFactoryFn ?? typeFactory).CreateInstance(req, serviceType) as IService;

                        ServiceExecFn serviceExec = (reqCtx, requestDto) =>
                                                    iserviceExec.Execute(reqCtx, service, requestDto);

                        return(ManagedServiceExec(serviceExec, service, req, dto));
                    };
                    AddToRequestExecMap(requestType, serviceType, handlerFn);
                    foreach (var item in actionMap[requestType])
                    {
                        AppHost.Metadata.Add(serviceType, requestType, item.ResponseType);

                        if (Logger.IsDebugEnabled)
                        {
                            Logger.DebugFormat("Registering {0} service '{1}' with request '{2}'",
                                               item.ResponseType != null ? "Reply" : "OneWay", serviceType.GetOperationName(), requestType.GetOperationName());
                        }
                    }

                    RegisterRestPaths(requestType);
                    if (typeof(IRequiresRequestStream).IsAssignableFrom(requestType))
                    {
                        this.RequestTypeFactoryMap[requestType] = req =>
                        {
                            var restPath = req.GetRoute();
                            var request  = restPath != null
                                ? RestHandler.CreateRequest(req, restPath, req.GetRequestParams(), requestType.CreateInstance())
                                : KeyValueDataContractDeserializer.Instance.Parse(req.QueryString, requestType);

                            var rawReq = (IRequiresRequestStream)request;
                            rawReq.RequestStream = req.InputStream;
                            return(rawReq);
                        };
                    }
                }
                serviceActionMap[serviceType] = actionMap;
                AppHost.Container.RegisterAutoWiredType(serviceType);
            }
        }
Ejemplo n.º 4
0
        public void RegisterService(ITypeFactory serviceFactoryFn, Type serviceType)
        {
            var processedReqs = new HashSet <Type>();

            if (IsServiceType(serviceType))
            {
                foreach (var mi in serviceType.GetActions())
                {
                    var requestType = mi.GetParameters()[0].ParameterType;
                    if (processedReqs.Contains(requestType))
                    {
                        continue;
                    }
                    processedReqs.Add(requestType);

                    RegisterServiceExecutor(requestType, serviceType, serviceFactoryFn);

                    var returnMarker = requestType.GetTypeWithGenericTypeDefinitionOf(typeof(IReturn <>));
                    var responseType = returnMarker != null?
                                       returnMarker.GetGenericArguments()[0]
                                       : mi.ReturnType != typeof(object) && mi.ReturnType != typeof(void) ?
                                       mi.ReturnType
                        : AssemblyUtils.FindType(requestType.FullName + ResponseDtoSuffix);

                    RegisterRestPaths(requestType);

                    appHost.Metadata.Add(serviceType, requestType, responseType);

                    if (typeof(IRequiresRequestStream).IsAssignableFrom(requestType))
                    {
                        this.RequestTypeFactoryMap[requestType] = req =>
                        {
                            var restPath = req.GetRoute();
                            var request  = RestHandler.CreateRequest(req, restPath, req.GetRequestParams(), requestType.CreateInstance());

                            var rawReq = (IRequiresRequestStream)request;
                            rawReq.RequestStream = req.InputStream;
                            return(rawReq);
                        };
                    }

                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Registering {0} service '{1}' with request '{2}'",
                                        responseType != null ? "Reply" : "OneWay", serviceType.GetOperationName(), requestType.GetOperationName());
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void RegisterService(Type serviceType)
        {
            var processedReqs = new HashSet <Type>();

            var actions = ServiceExecGeneral.Reset(serviceType);

            var requiresRequestStreamTypeInfo = typeof(IRequiresRequestStream).GetTypeInfo();

            var appHost = ServiceStackHost.Instance;

            foreach (var mi in serviceType.GetActions())
            {
                var requestType = mi.GetParameters()[0].ParameterType;
                if (processedReqs.Contains(requestType))
                {
                    continue;
                }
                processedReqs.Add(requestType);

                ServiceExecGeneral.CreateServiceRunnersFor(requestType, actions);

                var returnMarker = requestType.GetTypeWithGenericTypeDefinitionOf(typeof(IReturn <>));
                var responseType = returnMarker != null?
                                   GetGenericArguments(returnMarker)[0]
                                   : mi.ReturnType != typeof(object) && mi.ReturnType != typeof(void) ?
                                   mi.ReturnType
                    : Type.GetType(requestType.FullName + "Response");

                RegisterRestPaths(requestType);

                appHost.Metadata.Add(serviceType, requestType, responseType);

                if (requiresRequestStreamTypeInfo.IsAssignableFrom(requestType.GetTypeInfo()))
                {
                    this.RequestTypeFactoryMap[requestType] = req =>
                    {
                        var restPath = req.GetRoute();
                        var request  = RestHandler.CreateRequest(req, restPath, req.GetRequestParams(), ServiceStackHost.Instance.CreateInstance(requestType));

                        var rawReq = (IRequiresRequestStream)request;
                        rawReq.RequestStream = req.InputStream;
                        return(rawReq);
                    };
                }
            }
        }
        public void Can_deserialize_TestRequest_QueryStringSerializer_output()
        {
            // Setup
            using (new BasicAppHost(typeof (TestService).Assembly).Init())
            {
                var restPath = new RestPath(typeof(TestRequest), "/service", "GET");
                var restHandler = new RestHandler { RestPath = restPath };

                var requestString = "ListOfA={ListOfB:[{Property:prop1},{Property:prop2}]}";
                NameValueCollection queryString = HttpUtility.ParseQueryString(requestString);
                var httpReq = new MockHttpRequest("service", "GET", "application/json", "service", queryString, new MemoryStream(), new NameValueCollection());

                var request2 = (TestRequest)restHandler.CreateRequest(httpReq, "service");

                Assert.That(request2.ListOfA.Count, Is.EqualTo(1));
                Assert.That(request2.ListOfA.First().ListOfB.Count, Is.EqualTo(2));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// For performance withPathInfoParts should already be a lower case string
        /// to minimize redundant matching operations.
        /// </summary>
        /// <returns></returns>
        public bool IsMatch(IHttpRequest httpReq)
        {
            var pathInfo = httpReq.PathInfo;

            var matchFn = GetRequestRule();

            if (matchFn != null)
            {
                var validRoute = matchFn(httpReq);
                if (!validRoute)
                {
                    return(false);
                }
            }

            pathInfo = RestHandler.GetSanitizedPathInfo(pathInfo, out var contentType);

            var pathInfoParts = GetPathPartsForMatching(pathInfo);

            return(IsMatch(httpReq.HttpMethod, pathInfoParts, out var wildcardMatchCount));
        }
        public object Execute(IRequest req, bool applyFilters)
        {
            try
            {
                req.SetInProcessRequest();

                var restPath = HostContext.ServiceController.GetRestPathForRequest(req.Verb, req.PathInfo, req as IHttpRequest);

                req.OperationName = restPath.RequestType.GetOperationName();
                var task = RestHandler.CreateRequestAsync(req, restPath);
                task.Wait();
                var requestDto = task.Result;
                req.Dto = requestDto;

                if (applyFilters)
                {
                    requestDto = AppHost.ApplyRequestConverters(req, requestDto);

                    AppHost.ApplyRequestFiltersAsync(req, req.Response, requestDto).Wait();
                    if (req.Response.IsClosed)
                    {
                        return(null);
                    }
                }

                var response = Execute(requestDto, req);

                return(applyFilters
                    ? ApplyResponseFilters(response, req)
                    : response);
            }
            finally
            {
                req.ReleaseIfInProcessRequest();
            }
        }
Ejemplo n.º 9
0
        public RestPath FindRoute(string pathInfo, string method = HttpMethods.Get)
        {
            var route = RestHandler.FindMatchingRestPath(method, pathInfo, out _);

            return((RestPath)route);
        }