using Grpc.Core; using Grpc.Core.Interceptors; using System; using System.Text; using System.Threading.Tasks; namespace HotelPms.Data.Util { public class GrpcClientInterceptor : Interceptor { /// /// 一元调用(UnaryCall) /// /// /// /// /// /// /// public override AsyncUnaryCall AsyncUnaryCall(TRequest request, ClientInterceptorContext context, AsyncUnaryCallContinuation continuation) { Console.WriteLine("AsyncUnaryCall"); Console.WriteLine("客户端调用执行开始"); var responseCon = continuation(request, context); var response = new AsyncUnaryCall(responseCon.ResponseAsync, responseCon.ResponseHeadersAsync, responseCon.GetStatus, responseCon.GetTrailers, responseCon.Dispose); Console.WriteLine("客户端调用执行结束"); //非同期処理のため、ここには処理未完成 return response; } private async Task MyAsyncStuff(AsyncUnaryCall responseAsync) { return await responseAsync; } public override AsyncClientStreamingCall AsyncClientStreamingCall(ClientInterceptorContext context, AsyncClientStreamingCallContinuation continuation) { Console.WriteLine("AsyncClientStreamingCall"); return base.AsyncClientStreamingCall(context, continuation); } public override AsyncDuplexStreamingCall AsyncDuplexStreamingCall(ClientInterceptorContext context, AsyncDuplexStreamingCallContinuation continuation) { Console.WriteLine("AsyncDuplexStreamingCall"); return base.AsyncDuplexStreamingCall(context, continuation); } /// /// 同期 /// /// /// /// /// /// /// public override TResponse BlockingUnaryCall(TRequest request, ClientInterceptorContext context, BlockingUnaryCallContinuation continuation) { Console.WriteLine("BlockingUnaryCall"); return base.BlockingUnaryCall(request, context, continuation); } public override AsyncServerStreamingCall AsyncServerStreamingCall(TRequest request, ClientInterceptorContext context, AsyncServerStreamingCallContinuation continuation) { Console.WriteLine("AsyncServerStreamingCall"); return base.AsyncServerStreamingCall(request, context, continuation); } public override Task ClientStreamingServerHandler(IAsyncStreamReader requestStream, ServerCallContext context, ClientStreamingServerMethod continuation) { Console.WriteLine("ClientStreamingServerHandler"); return base.ClientStreamingServerHandler(requestStream, context, continuation); } public override Task DuplexStreamingServerHandler(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context, DuplexStreamingServerMethod continuation) { Console.WriteLine("DuplexStreamingServerHandler"); return base.DuplexStreamingServerHandler(requestStream, responseStream, context, continuation); } public override Task ServerStreamingServerHandler(TRequest request, IServerStreamWriter responseStream, ServerCallContext context, ServerStreamingServerMethod continuation) { Console.WriteLine("ServerStreamingServerHandler"); return base.ServerStreamingServerHandler(request, responseStream, context, continuation); } public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) { Console.WriteLine("UnaryServerHandler"); return base.UnaryServerHandler(request, context, continuation); } } }