示例#1
0
        private static Func <object, object, EndpointAttributes, object> CallServiceExecuteGeneric(
            Type requestType, Type serviceType)
        {
            try
            {
                var requestDtoParam  = Expression.Parameter(typeof(object), "requestDto");
                var requestDtoStrong = Expression.Convert(requestDtoParam, requestType);

                var serviceParam  = Expression.Parameter(typeof(object), "serviceObj");
                var serviceStrong = Expression.Convert(serviceParam, serviceType);

                var attrsParam = Expression.Parameter(typeof(EndpointAttributes), "attrs");

                var mi = ServiceExec.GetExecMethodInfo(serviceType, requestType);

                Expression callExecute = Expression.Call(
                    mi, new Expression[] { serviceStrong, requestDtoStrong, attrsParam });

                var executeFunc = Expression.Lambda <Func <object, object, EndpointAttributes, object> >
                                      (callExecute, requestDtoParam, serviceParam, attrsParam).Compile();

                return(executeFunc);
            }
            catch (Exception)
            {
                //problems with MONO, using reflection for temp fix
                return(delegate(object request, object service, EndpointAttributes attrs)
                {
                    var mi = ServiceExec.GetExecMethodInfo(serviceType, requestType);
                    return mi.Invoke(null, new[] { service, request, attrs });
                });
            }
        }
        public void Register <TReq>(Func <IService <TReq> > invoker)
        {
            var           requestType = typeof(TReq);
            ServiceExecFn handlerFn   = (requestContext, dto) => {
                var service = invoker();

                InjectRequestContext(service, requestContext);

                return(ServiceExec <TReq> .Execute(
                           service, (TReq)dto,
                           requestContext != null?requestContext.EndpointAttributes : EndpointAttributes.None));
            };

            requestExecMap.Add(requestType, handlerFn);
        }