Exemplo n.º 1
0
        public JimuServiceEntry Parse(MethodInfo methodInfo, JimuServiceDesc desc)
        {
            var fastInvoker = FastInvoke.GetMethodInvoker(methodInfo);

            var service = new JimuServiceEntry
            {
                Descriptor = desc,
                Func       = (paras, payload) =>
                {
                    var instance   = GetInstance(null, methodInfo.DeclaringType, payload);
                    var parameters = new List <object>();
                    foreach (var para in methodInfo.GetParameters())
                    {
                        paras.TryGetValue(para.Name, out var value);
                        var paraType  = para.ParameterType;
                        var parameter = JimuHelper.ConvertType(value, paraType);
                        parameters.Add(parameter);
                    }

                    var result = fastInvoker(instance, parameters.ToArray());
                    return(Task.FromResult(result));
                }
            };

            return(service);
        }
Exemplo n.º 2
0
        public async Task Invoke(HttpContext context)
        {
            using (var sr = new StreamReader(context.Request.Body))
            {
                var body = sr.ReadToEnd();
                _logger.Debug($"received msg is: {body}");
                _message = (JimuTransportMsg)JimuHelper.ConvertType(body, typeof(JimuTransportMsg));
            }

            _logger.Debug($"begin handling msg: {_message.Id}");
            IResponse response    = new HttpResponse(context.Response, _logger);
            var       thisContext = new RemoteCallerContext(_message, _serviceEntryContainer, response, _logger);
            var       lastInvoke  = new RequestDel(async ctx =>
            {
                JimuRemoteCallResultData resultMessage = new JimuRemoteCallResultData();

                if (ctx.ServiceEntry == null)
                {
                    resultMessage.ExceptionMessage = $"can not find service {ctx.RemoteInvokeMessage.ServiceId}";
                    await response.WriteAsync(_message.Id, resultMessage);
                }
                else if (ctx.ServiceEntry.Descriptor.WaitExecution)
                {
                    await LocalServiceExecuteAsync(ctx.ServiceEntry, ctx.RemoteInvokeMessage, resultMessage);
                    await response.WriteAsync(_message.Id, resultMessage);
                }
                else
                {
                    await response.WriteAsync(_message.Id, resultMessage);
                    await Task.Factory.StartNew(async() =>
                    {
                        await LocalServiceExecuteAsync(ctx.ServiceEntry, ctx.RemoteInvokeMessage, resultMessage);
                    });
                }
            });


            foreach (var middleware in _middlewares)
            {
                lastInvoke = middleware(lastInvoke);
            }

            await lastInvoke(thisContext);

            //await _next(context);
        }
Exemplo n.º 3
0
        public async Task <T> InvokeAsync <T>(string serviceIdOrPath, IDictionary <string, object> paras, JimuPayload payload)
        {
            _logger.Debug($"begin to invoke service: {serviceIdOrPath}");
            var result = await InvokeAsync(serviceIdOrPath, paras, payload);

            if (!string.IsNullOrEmpty(result.ExceptionMessage))
            {
                _logger.Debug($"invoking service: {serviceIdOrPath} raising an error: {result.ExceptionMessage}");
                throw new Exception(result.ExceptionMessage);
            }
            if (!string.IsNullOrEmpty(result.ErrorCode))
            {
                _logger.Debug($"invoking service: {serviceIdOrPath} raising an error: errorcode {result.ErrorCode}, error: {result.ErrorMsg}");
                if (result.Result == null)
                {
                    return(default(T));
                }
            }
            if (result.Result == null)
            {
                _logger.Debug($"invoking service: {serviceIdOrPath} has null return.");
                return(default(T));
            }
            object value;

            if (result.Result is Task <T> task)
            {
                value = JimuHelper.ConvertType(task.Result, typeof(T));
            }
            else
            {
                value = JimuHelper.ConvertType(result.Result, typeof(T));
            }

            _logger.Debug($"finish invoking service: {serviceIdOrPath}.");
            return((T)value);
        }