Beispiel #1
0
        private Task <object> CallGrpcAsyncCore <TRequest, TResponse>(GrpcMethodHandlerInfo method, IDictionary <string, string> headers, IEnumerable <TRequest> requests) where TRequest : class where TResponse : class
        {
            Channel     channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            CallInvoker invoker = new MiddlewareCallInvoker(channel, MiddlewarePipeline);

            CallOptions option = CreateCallOptions(headers);

            Method <TRequest, TResponse> rpc = (Method <TRequest, TResponse>)method.Method;

            switch (rpc.Type)
            {
            case MethodType.Unary:

                Task <TResponse> taskUnary = AsyncUnaryCall(invoker, rpc, option, requests.FirstOrDefault());

                var s = taskUnary.Result;
                return(Task.FromResult <object>(taskUnary.Result));

            case MethodType.ClientStreaming:

                Task <TResponse> taskClientStreaming = AsyncClientStreamingCall(invoker, rpc, option, requests);

                return(Task.FromResult <object>(taskClientStreaming.Result));

            case MethodType.ServerStreaming:

                Task <IList <TResponse> > taskServerStreaming = AsyncServerStreamingCall(invoker, rpc, option, requests.FirstOrDefault());

                return(Task.FromResult <object>(taskServerStreaming.Result));

            case MethodType.DuplexStreaming:

                Task <IList <TResponse> > taskDuplexStreaming = AsyncDuplexStreamingCall(invoker, rpc, option, requests);

                return(Task.FromResult <object>(taskDuplexStreaming.Result));

            default:
                throw new NotSupportedException(string.Format("MethodType '{0}' is not supported.", rpc.Type));
            }
        }
Beispiel #2
0
        private static void Main(string[] args)//async
        {
            var pipeline = new PipelineBuilder()
                           //.Use<ExceptionMiddleware>()
                           //.Use<TimerMiddleware>()
                           //.Use<LoggingMiddleware>()
                           //.Use<TimeoutMiddleware>()
                           .Use <PolicyMiddleware>(new PolicyMiddlewareOptions
            {
                RetryTimes         = 2,
                TimoutMilliseconds = 100
            })
            ;

            //pipeline.Use<LoggingMiddleware>();// pipeline.UseWhen<LoggingMiddleware>(ctx => ctx.Context.Method.EndsWith("SayHello"));
            //pipeline.Use<TimeoutMiddleware>(new TimeoutMiddlewareOptions { TimoutMilliseconds = 1000 });
            //console logger
            pipeline.Use(async(ctx, next) =>
            {
                Console.WriteLine(ctx.Request.ToString());
                await next(ctx);
                Console.WriteLine(ctx.Response.ToString());
            });

            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
            MiddlewareCallInvoker callInvoker = new MiddlewareCallInvoker(channel, pipeline.Build());

            var clientApi = new Get.GetClient(callInvoker);

            var replyApi = clientApi.SayHello(new ContractsSample1.HelloApiDemo.HelloRequest {
                Name = " APII"
            });

            Console.WriteLine("Greeting: " + replyApi.Message);

            var client2 = new ProductBasicSrv.ProductBasicSrvClient(callInvoker);
            var request = new ProductBasicGetsRequest
            {
                PageIndex = 1,
                PageSize  = 10,
            };

            request.Items.Add(1);
            request.PType = ProductBasicGetsRequest.Types.PhoneType.Work;
            var result2 = client2.Gets(request);

            //var s = client2.Gets2();
            //s.RequestStream.WriteAsync
            //var sd = await client2.GetsAsync(request);
            foreach (var r in result2.Result)
            {
                Console.WriteLine($"{r.ProductId}|---------|{r.ProductName}");
            }

            var    client = new BuiltHelloDemoSrv.BuiltHelloDemoSrvClient(callInvoker);
            String user   = "******";

            var reply = client.SayHello(new ContractsSample1.HelloDemo.HelloRequest {
                Name = user
            });

            Console.WriteLine("Greeting: " + reply.Message);

            channel.ShutdownAsync().Wait();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }