示例#1
0
            public override async Task <Reply> Execute(Request request, ServerCallContext callContext)
            {
                // TODO: read-only stream wrap over a span
                // TODO: unsafe byte string
                // TODO: error handling?
                // TODO: operation context via HTTP headers
                // TODO: resource handling here.

                Guid operationId = _context.TracingContext.Id;

                foreach (var header in callContext.RequestHeaders)
                {
                    if (header.Key.Equals("X-Cache-Operation-Id", StringComparison.OrdinalIgnoreCase))
                    {
                        Guid.TryParse(header.Value, out operationId);
                        break;
                    }
                }

                var tracingContext   = new Context(operationId, _context.TracingContext.Logger);
                var operationContext = new OperationContext(tracingContext, token: callContext.CancellationToken);

                return(await operationContext.PerformNonResultOperationAsync(Tracer, async() =>
                {
                    var commandRequest = _serializationPool.Deserialize(request.Request_.ToByteArray(), reader => CommandRequest.Deserialize(reader));

                    try
                    {
                        var commandResponse = await _service.HandleAsync(commandRequest);
                        var serialized = _serializationPool.Serialize(commandResponse, (value, writer) => value.Serialize(writer));
                        return new Reply()
                        {
                            Reply_ = Google.Protobuf.ByteString.CopyFrom(serialized),
                        };
                    }
                    catch (Exception e)
                    {
                        // TODO: do this properly.
                        throw new RpcException(new Status(StatusCode.Internal, detail: e.ToString()), message: "Internal server error");
                    }
                }, traceErrorsOnly : true));
            }