예제 #1
0
        public async Task <object> Execute(object requestDto, IRequest req)
        {
            req.Dto = requestDto;
            var requestType = requestDto.GetType();

            req.OperationName = requestType.Name;

            var serviceType = ServiceStackHost.Instance.Metadata.GetServiceTypeByRequest(requestType);

            var service = ServiceStackHost.Instance.CreateInstance(serviceType);

            //var service = typeFactory.CreateInstance(serviceType);

            var serviceRequiresContext = service as IRequiresRequest;

            if (serviceRequiresContext != null)
            {
                serviceRequiresContext.Request = req;
            }

            if (req.Dto == null) // Don't override existing batched DTO[]
            {
                req.Dto = requestDto;
            }

            //Executes the service and returns the result
            var response = await ServiceExecGeneral.Execute(serviceType, req, service, requestDto, requestType.GetOperationName()).ConfigureAwait(false);

            return(response);
        }
예제 #2
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);
                    };
                }
            }
        }