public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context,
                                                                                        UnaryServerMethod <TRequest, TResponse> continuation)
        {
            try
            {
                var startTime = DateTime.UtcNow;
                var watch     = new Stopwatch();
                watch.Start();
                using (LogProvider.OpenMappedContext("grpcMethod", context.Method))
                    using (LogProvider.OpenMappedContext("peer", context.Peer))
                        using (LogProvider.OpenMappedContext("grpcStartTime", startTime.ToString("u")))
                        {
                            var response = await continuation.Invoke(request, context);

                            watch.Stop();
                            var elapsedMs = watch.ElapsedMilliseconds;
                            Logger.Info("{peer} {grpcMethod} {grpcCode} {grpcTimeMs}", context.Peer, context.Method, context.Status.StatusCode, elapsedMs);
                            return(response);
                        }
            }
            catch (Exception e)
            {
                Logger.Error($"Got exception in RPC {context.Method}: {e.Message}\n{e.StackTrace}");
                throw;
            }
        }
示例#2
0
        /// <summary>
        /// 拦截服务请求
        /// </summary>
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            Endpoint endpoint       = OwinContextReader.Current.GetEndpoint();
            bool     needAuthorize  = AspNetSetting.Authorized;
            bool     allowAnonymous = endpoint.Metadata.GetMetadata <AllowAnonymousAttribute>() != null;

            if (needAuthorize && !allowAnonymous)
            {
                string         headerKey   = SessionKey.PublicKey.ToLower();
                Metadata.Entry headerEntry = context.RequestHeaders.Get(headerKey);
                string         publicKey   = headerEntry?.Value;
                if (string.IsNullOrWhiteSpace(publicKey))
                {
                    string message = "身份认证消息头不存在,请检查程序!";
                    NoPermissionException innerException = new NoPermissionException(message);
                    Status status = new Status(StatusCode.Unauthenticated, message, innerException);
                    throw new RpcException(status);
                }

                LoginInfo loginInfo = CacheMediator.Get <LoginInfo>(publicKey);
                if (loginInfo == null)
                {
                    string message = "身份过期,请重新登录!";
                    NoPermissionException innerException = new NoPermissionException(message);
                    Status status = new Status(StatusCode.Unauthenticated, message, innerException);
                    throw new RpcException(status);
                }

                //通过后,重新设置缓存过期时间
                CacheMediator.Set(publicKey, loginInfo, DateTime.Now.AddMinutes(GlobalSetting.AuthenticationTimeout));
            }

            return(await continuation.Invoke(request, context));
        }
示例#3
0
 public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
 {
     try
     {
         return(await continuation.Invoke(request, context));
     }
     catch (RpcException)
     {
         throw;
     }
     catch (Exception ex)
     {
         Log.Logger.Here().Error(ex, $"Unexpected error in gRPC handler for {type.ToString()}.");
         throw;
     }
 }
 /// <summary>
 /// 拦截服务请求
 /// </summary>
 public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
 {
     try
     {
         return(await continuation.Invoke(request, context));
     }
     catch (RpcException)
     {
         throw;
     }
     catch (Exception exception)
     {
         Exception innerException = GetInnerException(exception);
         Status    status         = new Status(StatusCode.Aborted, innerException.Message, exception);
         throw new RpcException(status);
     }
 }
示例#5
0
        public Task HandleCall(IPacketSession session, InternalPacket packet)
        {
            var rpc = new RpcTunnel <TResponse, TRequest>(session, m_MethodInv, packet.MsgId);

            rpc.Start();

            return(Task.Factory.StartNew(() =>
            {
                using (rpc)
                {
                    TRequest request = PacketHandler.GetData(packet, m_Method.RequestMarshaller);

                    m_Handler
                    .Invoke(request, new ServerCallContext())
                    .ContinueWith((taskResult) => rpc.Close(taskResult))
                    .Wait();
                }
            }));
        }
示例#6
0
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request,
                                                                                        ServerCallContext context,
                                                                                        UnaryServerMethod <TRequest, TResponse> continuation)
        {
            try
            {
                return(await continuation.Invoke(request, context));
            }
            catch (RpcException)
            {
                throw;
            }
            catch (Exception exception)
            {
                var exceptionType = exception.GetType();
                if (_mapping.ContainsKey(exceptionType))
                {
                    throw new RpcException(new Status(_mapping[exceptionType], exception.Message));
                }

                _logger.Warn($"Caught unmapped exception: {exception}");
                throw new RpcException(new Status(StatusCode.Unknown, "Internal Server Error"));
            }
        }