Ejemplo n.º 1
0
        public async Task Invoke(HttpContext httpContext)
        {
            object result         = null;
            var    errMessage     = string.Empty;
            var    httpStatusCode = HttpStatusCode.OK;
            var    buildRequest   = grpcRequestBuilder.BuildRequest(httpContext);

            if (buildRequest.IsError)
            {
                errMessage     = "bad request";
                httpStatusCode = HttpStatusCode.BadRequest;
                Logger.LogWarning(errMessage);
            }
            else
            {
                try
                {
                    var channel = grpcPool.GetChannel(new ServiceEndpoint(httpContext.Items.DownstreamRequest().Host, httpContext.Items.DownstreamRequest().Port));
                    var client  = new MethodDescriptorClient(channel);
                    result = await client.InvokeAsync(buildRequest.Data.GrpcMethod, buildRequest.Data.Headers, buildRequest.Data.RequestMessage);
                }
                catch (RpcException ex)
                {
                    httpStatusCode = HttpStatusCode.InternalServerError;
                    errMessage     = $"rpc exception.";
                    Logger.LogError($"{ex.StatusCode}--{ex.Message}", ex);
                }
                catch (Exception ex)
                {
                    httpStatusCode = HttpStatusCode.ServiceUnavailable;
                    errMessage     = $"error in request grpc service.";
                    Logger.LogError($"{errMessage}--{httpContext.Items.DownstreamRequest().ToUri()}", ex);
                }
            }
            OkResponse <GrpcHttpContent> httpResponse;

            if (string.IsNullOrEmpty(errMessage))
            {
                httpResponse = new OkResponse <GrpcHttpContent>(new GrpcHttpContent(result));
            }
            else
            {
                httpResponse = new OkResponse <GrpcHttpContent>(new GrpcHttpContent(errMessage));
            }
            httpContext.Response.ContentType = "application/json";
            httpContext.Items.UpsertDownstreamResponse(new DownstreamResponse(httpResponse.Data, httpStatusCode, httpResponse.Data.Headers, "OcelotGrpcHttpMiddleware"));
            // httpContext.DownstreamResponse = new DownstreamResponse(httpResponse.Data, httpStatusCode, httpResponse.Data.Headers, "OcelotGrpcHttpMiddleware");
        }
Ejemplo n.º 2
0
        public void GetSslChannelTest()
        {
            IEnumerable <string> emptyScopes = Enumerable.Empty <string>();
            ServiceEndpoint      endpoint    = new ServiceEndpoint(TestCredentials.DefaultHostOverride, SslPort);
            var    pool    = new GrpcPool(emptyScopes);
            var    channel = pool.GetChannel(endpoint, clientCredentials);
            var    client  = new Greeter.GreeterClient(channel);
            String user    = "******";

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

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

            channel.ShutdownAsync().Wait();
            Assert.AreEqual(reply.Message, "leenux=Hello you");
        }