示例#1
0
        private async Task OnReceived(IChannelHandlerContext channel, JimuTransportMsg message)
        {
            _logger.Debug($"begin handling msg: {message.Id}");
            //TaskCompletionSource<TransportMessage> task;
            if (message.ContentType == typeof(JimuRemoteCallData).FullName)
            {
                IResponse response    = new DotNettyResponse(channel, _logger);
                var       thisContext = new ServiceInvokerContext(message, _serviceEntryContainer, response, _logger, _serviceInvokeAddress);
                Guid      operationId = Guid.Empty;

                var lastInvoke = new RequestDel(async context =>
                {
                    JimuRemoteCallResultData resultMessage = new JimuRemoteCallResultData();
                    if (context.ServiceEntry == null)
                    {
                        resultMessage.ExceptionMessage = $"can not find service {context.RemoteInvokeMessage.ServiceId}";
                        _jimuApm.WriteServiceInvokeAfter(operationId, thisContext, resultMessage);
                        await response.WriteAsync(message.Id, resultMessage);
                    }
                    else if (context.ServiceEntry.Descriptor.WaitExecution)
                    {
                        await LocalServiceExecuteAsync(context.ServiceEntry, context.RemoteInvokeMessage, resultMessage);
                        _jimuApm.WriteServiceInvokeAfter(operationId, thisContext, resultMessage);
                        await response.WriteAsync(message.Id, resultMessage);
                    }
                    else
                    {
                        await response.WriteAsync(message.Id, resultMessage);
                        await Task.Factory.StartNew(async() =>
                        {
                            await LocalServiceExecuteAsync(context.ServiceEntry, context.RemoteInvokeMessage, resultMessage);
                            _jimuApm.WriteServiceInvokeAfter(operationId, thisContext, resultMessage);
                        });
                    }
                });

                foreach (var middleware in _middlewares)
                {
                    lastInvoke = middleware(lastInvoke);
                }
                try
                {
                    _jimuApm.WriteServiceInvokeBefore(thisContext);
                    await lastInvoke(thisContext);
                }
                catch (Exception ex)
                {
                    JimuRemoteCallResultData resultMessage = new JimuRemoteCallResultData();
                    resultMessage.ErrorCode        = "500";
                    resultMessage.ExceptionMessage = ex.ToStackTraceString();
                    _logger.Error("throw exception when excuting local service: \r\n " + JimuHelper.Serialize <string>(message), ex);
                    await response.WriteAsync(message.Id, resultMessage);
                }
            }
            else
            {
                _logger.Debug($"msg: {message.Id}, message type is not an  JimuRemoteCallData.");
            }
        }
示例#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)_typeConvert.Convert(body, typeof(JimuTransportMsg));
            }

            _logger.Debug($"begin handling msg: {_message.Id}");
            IResponse response    = new HttpResponse(context.Response, _serializer, _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);
        }
示例#3
0
        private async Task OnReceived(IChannelHandlerContext channel, JimuTransportMsg message)
        {
            _logger.Debug($"begin handling msg: {message.Id}");
            //TaskCompletionSource<TransportMessage> task;
            if (message.ContentType == typeof(JimuRemoteCallData).FullName)
            {
                IResponse response    = new DotNettyResponse(channel, _serializer, _logger);
                var       thisContext = new RemoteCallerContext(message, _serviceEntryContainer, response, _logger);

                var lastInvoke = new RequestDel(async context =>
                {
                    JimuRemoteCallResultData resultMessage = new JimuRemoteCallResultData();
                    if (context.ServiceEntry == null)
                    {
                        resultMessage.ExceptionMessage = $"can not find service {context.RemoteInvokeMessage.ServiceId}";
                        await response.WriteAsync(message.Id, resultMessage);
                    }
                    else if (context.ServiceEntry.Descriptor.WaitExecution)
                    {
                        await LocalServiceExecuteAsync(context.ServiceEntry, context.RemoteInvokeMessage, resultMessage);
                        await response.WriteAsync(message.Id, resultMessage);
                    }
                    else
                    {
                        await response.WriteAsync(message.Id, resultMessage);
                        await Task.Factory.StartNew(async() =>
                        {
                            await LocalServiceExecuteAsync(context.ServiceEntry, context.RemoteInvokeMessage, resultMessage);
                        });
                    }
                });

                foreach (var middleware in _middlewares)
                {
                    lastInvoke = middleware(lastInvoke);
                }
                await lastInvoke(thisContext);
            }
            else
            {
                _logger.Debug($"msg: {message.Id}, message type is not an  JimuRemoteCallData.");
            }
        }
示例#4
0
        private async Task OnReceived(IChannelHandlerContext channel, TransportMsg message)
        {
            _logger.Debug($"开始触发服务: {message.Id}");
            if (message.ContentType == typeof(RemoteCallData).FullName)
            {
                IResponse             response    = new RpcResponse(channel, _serializer, _logger);
                RemoteExecutorContext thisContext = new RemoteExecutorContext(message, _serviceEntryContainer, response, _serializer, _logger, _serviceDiscovery);

                RequestDel lastInvoke = new RequestDel(async context =>
                {
                    RemoteCallBackData resultMessage = new RemoteCallBackData();
                    if (context.ServiceEntry == null)
                    {
                        resultMessage.ExceptionMessage = $"没有此服务:{context.RemoteInvokeMessage.ServiceId}";
                        await response.WriteAsync(message.Id, resultMessage);
                    }
                    else if (context.ServiceEntry.Descriptor.WaitExecution)
                    {
                        await LocalServiceExecuteAsync(context, resultMessage);
                        await response.WriteAsync(message.Id, resultMessage);
                    }
                    else
                    {
                        await response.WriteAsync(message.Id, resultMessage);
                        await Task.Factory.StartNew(async() =>
                        {
                            await LocalServiceExecuteAsync(context, resultMessage);
                        });
                    }
                });

                foreach (Func <RequestDel, RequestDel> middleware in _middlewares)
                {
                    lastInvoke = middleware.Invoke(lastInvoke);
                }
                await lastInvoke.Invoke(thisContext);
            }
            else
            {
                _logger.Debug($"msg: {message.Id}, message type is not an  RemoteCallData.");
            }
        }
示例#5
0
 public JwtAuthorizationMiddleware(RequestDel next, JwtAuthorizationOptions options, ISerializer serializer)
 {
     _options    = options;
     _serializer = serializer;
     _next       = next;
 }
示例#6
0
 private async Task InvokeMiddleware(RequestDel next, ServiceInvokerContext context)
 {
     await next.Invoke(context);
 }
 public JwtAuthorizationMiddleware(RequestDel next, JwtAuthorizationOptions options, IContainer container)
 {
     _options   = options;
     _next      = next;
     _container = container;
 }
示例#8
0
 private async Task InvokeMiddleware(RequestDel next, RemoteCallerContext context)
 {
     await next.Invoke(context);
 }