public static ServerServiceDefinition BindHandler(DuplexStreamingServerMethod <byte[], byte[]> handler) { return(ServerServiceDefinition.CreateBuilder() .AddMethod(StreamingCallMethod, handler).Build()); }
public override Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { return(continuation(requestStream, responseStream, context)); }
public override async Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { EnsureIncomingOrder(context); await continuation(requestStream, responseStream, context).DefaultTimeout(); EnsureOutgoingOrder(context); }
public DuplexStreamingServerCallHandler(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TService, TRequest, TResponse> invoker, GrpcServiceOptions serviceOptions, ILoggerFactory loggerFactory) : base(method, serviceOptions, loggerFactory) { _invoker = invoker; }
public override async Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { await Monitor <TRequest, TResponse>(requestStream, context, continuation, responseStream); }
public MockServiceHelper(string host = null) { this.host = host ?? "localhost"; serviceDefinition = ServerServiceDefinition.CreateBuilder(ServiceName) .AddMethod(UnaryMethod, (request, context) => unaryHandler(request, context)) .AddMethod(ClientStreamingMethod, (requestStream, context) => clientStreamingHandler(requestStream, context)) .AddMethod(ServerStreamingMethod, (request, responseStream, context) => serverStreamingHandler(request, responseStream, context)) .AddMethod(DuplexStreamingMethod, (requestStream, responseStream, context) => duplexStreamingHandler(requestStream, responseStream, context)) .Build(); var defaultStatus = new Status(StatusCode.Unknown, "Default mock implementation. Please provide your own."); unaryHandler = new UnaryServerMethod<string, string>(async (request, context) => { context.Status = defaultStatus; return ""; }); clientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => { context.Status = defaultStatus; return ""; }); serverStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) => { context.Status = defaultStatus; }); duplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) => { context.Status = defaultStatus; }); }
public override void AddMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TRequest, TResponse> handler) { if (TryGetMethodDescriptor(method.Name, out var methodDescriptor) && TryGetHttpRule(methodDescriptor, out _)) { Log.StreamingMethodNotSupported(_logger, method.Name, typeof(TService)); } }
/// <inheritdoc/> public override async Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { try { await base.DuplexStreamingServerHandler(requestStream, responseStream, context, continuation).ConfigureAwait(false); } catch (Exception ex) when(OnException(ex, out var status)) { throw new RpcException(status, ex.Message); } }
public override Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { var methodName = context.Method.Split('/').Last(); var callMethod = _methods.FirstOrDefault(p => p.Name == methodName); var serviceScopeFactory = _serviceProvider.GetService <IServiceScopeFactory>(); using (var scope = serviceScopeFactory.CreateScope()) { var serviceInstance = scope.ServiceProvider.GetService(serviceType); var serviceResult = callMethod.Invoke(serviceInstance, new object[] { requestStream, responseStream, context }); return(serviceResult as Task); } }
public DuplexStreamingServerCallHandler <TService, TRequest, TResponse> CreateDuplexStreaming <TRequest, TResponse>(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TService, TRequest, TResponse> invoker) where TRequest : class where TResponse : class { var options = CreateMethodOptions(); var methodInvoker = new DuplexStreamingServerMethodInvoker <TService, TRequest, TResponse>(invoker, method, options, _serviceActivator); return(new DuplexStreamingServerCallHandler <TService, TRequest, TResponse>(methodInvoker, _loggerFactory)); }
protected override async Task HandleCallAsyncCore(HttpContext httpContext) { var serverCallContext = CreateServerCallContext(httpContext); GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response); var activator = httpContext.RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >(); TService service = null; try { serverCallContext.Initialize(); if (ServiceOptions.Interceptors.IsEmpty) { try { service = activator.Create(); await _invoker( service, new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer), new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer), serverCallContext); } finally { if (service != null) { activator.Release(service); } } } else { DuplexStreamingServerMethod <TRequest, TResponse> resolvedInvoker = async(requestStream, responseStream, resolvedContext) => { try { service = activator.Create(); await _invoker( service, requestStream, responseStream, resolvedContext); } finally { if (service != null) { activator.Release(service); } } }; // The list is reversed during construction so the first interceptor is built last and invoked first for (var i = ServiceOptions.Interceptors.Count - 1; i >= 0; i--) { resolvedInvoker = BuildInvoker(ServiceOptions.Interceptors[i], resolvedInvoker); } await resolvedInvoker( new HttpContextStreamReader <TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer), new HttpContextStreamWriter <TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer), serverCallContext); } await serverCallContext.EndCallAsync(); } catch (Exception ex) { serverCallContext.ProcessHandlerError(ex, Method.Name); } finally { serverCallContext.Dispose(); } }
public DuplexStreamingServerCallHandler <TService, TRequest, TResponse> CreateDuplexStreaming <TRequest, TResponse>(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TService, TRequest, TResponse> invoker) where TRequest : class where TResponse : class { return(new DuplexStreamingServerCallHandler <TService, TRequest, TResponse>(method, invoker, _resolvedOptions, _loggerFactory)); }
public override async Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { CheckConcurrentLimit(); try { await continuation(requestStream, responseStream, context); } finally { _concurrentCalls.Release(); } }
public override async Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { (string, string)tuple = InterceptCallContext(context); var watch = Stopwatch.StartNew(); await continuation(requestStream, responseStream, context); Logger.Log.GrpcTrace(m_loggingContext, string.Format(RespondedLogFormat, tuple.Item1, tuple.Item2, watch.ElapsedMilliseconds)); }
public DuplexStreamingServerCallHandler(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TRequest, TResponse> handler) { this.method = method; this.handler = handler; }
public override void AddMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TRequest, TResponse> handler) { var callHandler = _serverCallHandlerFactory.CreateDuplexStreaming(method, _serviceInvokerFactory.CreateDuplexStreamingInvoker(method)); EndpointConventionBuilders.Add(_builder.MapPost(method.FullName, callHandler.HandleCallAsync)); }
public override async Task DuplexStreamingServerHandler <TRequest, TResponse>(IAsyncStreamReader <TRequest> requestStream, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod <TRequest, TResponse> continuation) { if (ThrottleManager.Instance.IsThrottled(context.Method)) { throw new RpcException(new Status( StatusCode.Cancelled, Newtonsoft.Json.JsonConvert.SerializeObject(new { Code = 503, Detail = Consts.ThrottledMsg }))); } await continuation(requestStream, responseStream, context); }
/// <summary> /// Adds a duplex streaming method to a service. /// </summary> /// <typeparam name="TRequest">Request message type for this method.</typeparam> /// <typeparam name="TResponse">Response message type for this method.</typeparam> /// <param name="method">The method description.</param> /// <param name="metadata">The method metadata. This metadata can be used by routing and middleware when invoking a gRPC method.</param> /// <param name="invoker">The method invoker that is executed when the method is called.</param> public void AddDuplexStreamingMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, IList <object> metadata, DuplexStreamingServerMethod <TService, TRequest, TResponse> invoker) where TRequest : class where TResponse : class { var callHandler = _serverCallHandlerFactory.CreateDuplexStreaming <TRequest, TResponse>(method, invoker); var methodModel = new MethodModel(method, metadata, callHandler.HandleCallAsync); Methods.Add(methodModel); }
public MockServiceHelper(string host = null, Marshaller <string> marshaller = null, IEnumerable <ChannelOption> channelOptions = null) { this.host = host ?? "localhost"; this.channelOptions = channelOptions; marshaller = marshaller ?? Marshallers.StringMarshaller; unaryMethod = new Method <string, string>( MethodType.Unary, ServiceName, "Unary", marshaller, marshaller); clientStreamingMethod = new Method <string, string>( MethodType.ClientStreaming, ServiceName, "ClientStreaming", marshaller, marshaller); serverStreamingMethod = new Method <string, string>( MethodType.ServerStreaming, ServiceName, "ServerStreaming", marshaller, marshaller); duplexStreamingMethod = new Method <string, string>( MethodType.DuplexStreaming, ServiceName, "DuplexStreaming", marshaller, marshaller); serviceDefinition = ServerServiceDefinition.CreateBuilder() .AddMethod(unaryMethod, (request, context) => unaryHandler(request, context)) .AddMethod(clientStreamingMethod, (requestStream, context) => clientStreamingHandler(requestStream, context)) .AddMethod(serverStreamingMethod, (request, responseStream, context) => serverStreamingHandler(request, responseStream, context)) .AddMethod(duplexStreamingMethod, (requestStream, responseStream, context) => duplexStreamingHandler(requestStream, responseStream, context)) .Build(); var defaultStatus = new Status(StatusCode.Unknown, "Default mock implementation. Please provide your own."); unaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { context.Status = defaultStatus; return(""); }); clientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) => { context.Status = defaultStatus; return(""); }); serverStreamingHandler = new ServerStreamingServerMethod <string, string>(async(request, responseStream, context) => { context.Status = defaultStatus; }); duplexStreamingHandler = new DuplexStreamingServerMethod <string, string>(async(requestStream, responseStream, context) => { context.Status = defaultStatus; }); }
public static ServerServiceDefinition BindHandler(DuplexStreamingServerMethod<byte[], byte[]> handler) { return ServerServiceDefinition.CreateBuilder() .AddMethod(StreamingCallMethod, handler).Build(); }
public Method <TRequest, TResponse> AddDuplexStreamingMethod <TRequest, TResponse>(DuplexStreamingServerMethod <TRequest, TResponse> callHandler, string?methodName = null) where TRequest : class, IMessage, new() where TResponse : class, IMessage, new() { var method = CreateMethod <TRequest, TResponse>(MethodType.DuplexStreaming, methodName ?? Guid.NewGuid().ToString()); AddServiceCore(c => { c.AddDuplexStreamingMethod(method, new List <object>(), new DuplexStreamingServerMethod <DynamicService, TRequest, TResponse>((service, input, output, context) => callHandler(input, output, context))); }); return(method); }
public MockServiceHelper(string host = null, Marshaller<string> marshaller = null, IEnumerable<ChannelOption> channelOptions = null) { this.host = host ?? "localhost"; this.channelOptions = channelOptions; marshaller = marshaller ?? Marshallers.StringMarshaller; unaryMethod = new Method<string, string>( MethodType.Unary, ServiceName, "Unary", marshaller, marshaller); clientStreamingMethod = new Method<string, string>( MethodType.ClientStreaming, ServiceName, "ClientStreaming", marshaller, marshaller); serverStreamingMethod = new Method<string, string>( MethodType.ServerStreaming, ServiceName, "ServerStreaming", marshaller, marshaller); duplexStreamingMethod = new Method<string, string>( MethodType.DuplexStreaming, ServiceName, "DuplexStreaming", marshaller, marshaller); serviceDefinition = ServerServiceDefinition.CreateBuilder() .AddMethod(unaryMethod, (request, context) => unaryHandler(request, context)) .AddMethod(clientStreamingMethod, (requestStream, context) => clientStreamingHandler(requestStream, context)) .AddMethod(serverStreamingMethod, (request, responseStream, context) => serverStreamingHandler(request, responseStream, context)) .AddMethod(duplexStreamingMethod, (requestStream, responseStream, context) => duplexStreamingHandler(requestStream, responseStream, context)) .Build(); var defaultStatus = new Status(StatusCode.Unknown, "Default mock implementation. Please provide your own."); unaryHandler = new UnaryServerMethod<string, string>(async (request, context) => { context.Status = defaultStatus; return ""; }); clientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => { context.Status = defaultStatus; return ""; }); serverStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) => { context.Status = defaultStatus; }); duplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) => { context.Status = defaultStatus; }); }
public override void AddMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, DuplexStreamingServerMethod <TRequest, TResponse> handler) { MetaModel.Methods.Add((new MetaMethodModel { FullName = method.FullName, RequestType = typeof(TRequest), ResponseType = typeof(TResponse), Handler = handler })); }