public static void TestMain(string[] args)
        {
            GrpcClientHelper.SafeInvoke("127.0.0.1", 50051, channel =>
            {
                Greeter.GreeterClient client = new Greeter.GreeterClient(channel);
                var result = client.SayHello(new Request(), deadline: DateTime.UtcNow.AddSeconds(12));
                Console.WriteLine(result.Response_);
            });


            GrpcClientHelper.SafeInvoke2 <Greeter.GreeterClient>("127.0.0.1", 50051, client =>
            {
                var result = client.SayHello(new Request(), deadline: DateTime.UtcNow.AddSeconds(12));
                Console.WriteLine(result.Response_);
            });



            GrpcClientHelper.SafeInvokeWithSsl("192.168.163.159", 5142, channel =>
            {
                var client        = new Greeter.GreeterClient(channel);
                Request request   = new Request();
                request.Request_  = "request at" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
                Response response = client.SayHello(request, deadline: DateTime.UtcNow.AddSeconds(36));
                Console.WriteLine(response.Response_);
            });
        }
예제 #2
0
        /// <summary>
        /// 一元调用
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <param name="continuation"></param>
        /// <returns></returns>
        public override Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            //调用的方法名
            string methodName = GrpcClientHelper.GetClientCallMethod(context);
            //客户端的IP
            string clientIp = GrpcClientHelper.GetClientIp(context);
            //开始调用时间
            DateTime startTime = DateTime.Now;

            try
            {
                this._beforeAction?.Invoke(methodName, clientIp);
            }
            catch
            {
                // ignored
            }

            try
            {
                Task <TResponse> responseTask = continuation(request, context);

                //调用使用时间
                TimeSpan usedTimeSpan = DateTime.Now - startTime;
                try
                {
                    _afterAction?.Invoke(methodName, clientIp, true, null, usedTimeSpan);
                }
                catch
                {
                    // ignored
                }


                return(responseTask);
            }
            catch (Exception ex)
            {
                //调用使用时间
                TimeSpan usedTimeSpan = DateTime.Now - startTime;
                try
                {
                    _afterAction?.Invoke(methodName, clientIp, false, ex, usedTimeSpan);
                }
                catch
                {
                    // ignored
                }

                throw;
            }
        }