public ServerStreamingServerCallHandler( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TService, TRequest, TResponse> invoker, GrpcServiceOptions serviceOptions, ILoggerFactory loggerFactory) : base(method, serviceOptions, loggerFactory) { _invoker = invoker; if (!ServiceOptions.Interceptors.IsEmpty) { ServerStreamingServerMethod <TRequest, TResponse> resolvedInvoker = async(resolvedRequest, responseStream, resolvedContext) => { var activator = resolvedContext.GetHttpContext().RequestServices.GetRequiredService <IGrpcServiceActivator <TService> >(); TService?service = null; try { service = activator.Create(); await _invoker(service, resolvedRequest, responseStream, resolvedContext); } finally { if (service != null) { activator.Release(service); } } }; var interceptorPipeline = new InterceptorPipelineBuilder <TRequest, TResponse>(ServiceOptions.Interceptors); _pipelineInvoker = interceptorPipeline.ServerStreamingPipeline(resolvedInvoker); } }
/// <inheritdoc/> public override async Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { using var rpcScope = new ServerRpcScope <TRequest, TResponse>(context, this.options); try { rpcScope.RecordRequest(request); var responseStreamProxy = new ServerStreamWriterProxy <TResponse>( responseStream, rpcScope.RecordResponse); await continuation(request, responseStreamProxy, context).ConfigureAwait(false); rpcScope.Complete(); } catch (Exception e) { rpcScope.CompleteWithException(e); throw; } }
public ServerStreamingServerCallHandler( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TService, TRequest, TResponse> invoker, GrpcServiceOptions serviceOptions, ILoggerFactory loggerFactory, IGrpcServiceActivator <TService> serviceActivator, IServiceProvider serviceProvider) : base(method, serviceOptions, loggerFactory, serviceActivator, serviceProvider) { _invoker = invoker; if (ServiceOptions.HasInterceptors) { ServerStreamingServerMethod <TRequest, TResponse> resolvedInvoker = async(resolvedRequest, responseStream, resolvedContext) => { GrpcActivatorHandle <TService> serviceHandle = default; try { serviceHandle = ServiceActivator.Create(resolvedContext.GetHttpContext().RequestServices); await _invoker(serviceHandle.Instance, resolvedRequest, responseStream, resolvedContext); } finally { if (serviceHandle.Instance != null) { ServiceActivator.Release(serviceHandle); } } }; var interceptorPipeline = new InterceptorPipelineBuilder <TRequest, TResponse>(ServiceOptions.Interceptors, ServiceProvider); _pipelineInvoker = interceptorPipeline.ServerStreamingPipeline(resolvedInvoker); } }
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; }); }
/// <summary> /// Adds a definition for a server streaming method. /// </summary> /// <typeparam name="TRequest">The request message class.</typeparam> /// <typeparam name="TResponse">The response message class.</typeparam> /// <param name="method">The method.</param> /// <param name="handler">The method handler.</param> public virtual void AddMethod <TRequest, TResponse>( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TRequest, TResponse> handler) where TRequest : class where TResponse : class { throw new NotImplementedException(); }
public override void AddMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, ServerStreamingServerMethod <TRequest, TResponse> handler) { m_context.AddServerStreamingMethod(method, Array.Empty <object>(), (service, request, stream, context) => handler(request, stream, context)); m_logger.Log(LogLevel.Information, "RPC service being provided by {0}: {1}", typeof(TService), method.Name); }
public override Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { return(continuation(request, responseStream, context)); }
public override Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { throw new RpcException(new Status(StatusCode.Unimplemented, "secure streaming calls are not supported")); }
public override void AddMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, ServerStreamingServerMethod <TRequest, TResponse> handler) { if (TryGetMethodDescriptor(method.Name, out var methodDescriptor) && ServiceDescriptorHelpers.TryGetHttpRule(methodDescriptor, out _)) { Log.StreamingMethodNotSupported(_logger, method.Name, typeof(TService)); } }
public void AddServerStreamingMethod <TRequest, TResponse>( GrpcCore.Method <TRequest, TResponse> method, ImmutableArray <object> metadata, ServerStreamingServerMethod <NetGrpcServiceActivator <TService>, TRequest, TResponse> handler) where TRequest : class where TResponse : class { this.stubs.Add(new TestGrpcMethodStub(method, typeof(TRequest), typeof(TResponse), metadata)); }
public override Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { LogCall <TRequest, TResponse>(MethodType.ServerStreaming, context); return(base.ServerStreamingServerHandler(request, responseStream, context, continuation)); }
/// <summary> /// Adds a definitions for a server streaming method. /// </summary> /// <typeparam name="TRequest">The request message class.</typeparam> /// <typeparam name="TResponse">The response message class.</typeparam> /// <param name="method">The method.</param> /// <param name="handler">The method handler.</param> /// <returns>This builder instance.</returns> public Builder AddMethod <TRequest, TResponse>( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TRequest, TResponse> handler) where TRequest : class where TResponse : class { callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler)); return(this); }
public ServerStreamingServerCallHandler( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TService, TRequest, TResponse> invoker, GrpcServiceOptions serviceOptions, ILoggerFactory loggerFactory) : base(method, serviceOptions, loggerFactory) { _invoker = invoker; }
public void AddServerStreamingMethod <TRequest, TResponse>( Method <TRequest, TResponse> create, ImmutableArray <object> metadata, ServerStreamingServerMethod <NetGrpcServiceActivator <TService>, TRequest, TResponse> handler) where TRequest : class where TResponse : class { this.context.AddServerStreamingMethod(create, TranslateMetadata(metadata), handler); }
public async Task WriteUntilDeadline_SuccessResponsesStreamed_CoreAsync(ServerStreamingServerMethod <HelloRequest, HelloReply> method) { // Arrange var url = Fixture.DynamicGrpc.AddServerStreamingMethod <DeadlineTests, HelloRequest, HelloReply>(method); var requestMessage = new HelloRequest { Name = "World" }; var requestStream = new MemoryStream(); MessageHelpers.WriteMessage(requestStream, requestMessage); var httpRequest = new HttpRequestMessage(HttpMethod.Post, url); httpRequest.Headers.Add(GrpcProtocolConstants.TimeoutHeader, "200m"); httpRequest.Content = new GrpcStreamContent(requestStream); // Act var response = await Fixture.Client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout(); // Assert response.AssertIsSuccessfulGrpcRequest(); var responseStream = await response.Content.ReadAsStreamAsync().DefaultTimeout(); var pipeReader = new StreamPipeReader(responseStream); var messageCount = 0; var readTask = Task.Run(async() => { while (true) { var greeting = await MessageHelpers.AssertReadStreamMessageAsync <HelloReply>(pipeReader).DefaultTimeout(); if (greeting != null) { Assert.AreEqual($"How are you World? {messageCount}", greeting.Message); messageCount++; } else { break; } } }); await readTask.DefaultTimeout(); Assert.AreNotEqual(0, messageCount); Assert.AreEqual(StatusCode.DeadlineExceeded.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].Single()); Assert.AreEqual("Deadline Exceeded", Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.MessageTrailer].Single()); }
/// <summary> /// ServerStreaming メソッドを生成します。 /// </summary> /// <typeparam name="TRequest">リクエストの型</typeparam> /// <typeparam name="TResponse">レスポンスの型</typeparam> /// <param name="builderContext">コンテキスト</param> /// <returns>メソッド</returns> private ServerStreamingServerMethod <TRequest, TResponse> CreateServerStreamingServerMethod <TRequest, TResponse>(MethodBuildContext builderContext) where TRequest : class where TResponse : class { ServerStreamingServerMethod <TRequest, TResponse> method = builderContext.MethodImpl.CreateDelegate(typeof(ServerStreamingServerMethod <TRequest, TResponse>), builderContext.ServiceInstance) as ServerStreamingServerMethod <TRequest, TResponse>; GrpcServerPerformanceListener performanceListener = builderContext.NeedNotifyPerformanceLog ? builderContext.Settings.PerformanceListener : null; return(async delegate(TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context) { try { await OnExecutingServiceMethodAsync(context, builderContext.InvokingInterceptors, performanceListener).ConfigureAwait(false); if (performanceListener != null) { performanceListener.NotifyMethodCalling(context); } Stopwatch watch = Stopwatch.StartNew(); double elapsd; try { await method(request, new ResponseStreamWriter <TResponse>(responseStream, context, performanceListener), context).ConfigureAwait(false); } catch (Exception ex) { throw new GrpcServerMethodException(string.Format(Properties.MessageResources.ServerMethodFailed, context.Method) + ex.Message, ex, context); } finally { elapsd = GrpcPerformanceListener.GetMilliseconds(watch); if (performanceListener != null) { performanceListener.NotifyMethodCalled(context, elapsd); } } await OnExecutedServiceMethodAsync(context, builderContext.InvokedInterceptors, performanceListener).ConfigureAwait(false); } catch (Exception ex) { Exception wrapped; if (HandleException(context, builderContext.ExceptionHandlers, performanceListener, ex, out wrapped)) { GrpcExceptionListener.NotifyCatchServerException(context, wrapped); throw wrapped; } else { GrpcExceptionListener.NotifyCatchServerException(context, ex); throw; } } }); }
public override Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { SetCultureFromMetadata(context); return(base.ServerStreamingServerHandler(request, responseStream, context, continuation)); }
/// <summary> /// Adds a definition for a server streaming method. /// </summary> /// <typeparam name="TRequest">The request message class.</typeparam> /// <typeparam name="TResponse">The response message class.</typeparam> /// <param name="method">The method.</param> /// <param name="handler">The method handler.</param> /// <returns>This builder instance.</returns> public Builder AddMethod <TRequest, TResponse>( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TRequest, TResponse> handler) where TRequest : class where TResponse : class { duplicateDetector.Add(method.FullName, null); addMethodActions.Add((serviceBinder) => serviceBinder.AddMethod(method, handler)); return(this); }
private ServerStreamingServerMethod <TRequest, TResponse> getServerStreamingServerHandlerChain <TRequest, TResponse>(Interceptor interceptor, ServerStreamingServerMethod <TRequest, TResponse> continuation) where TRequest : class where TResponse : class { ServerStreamingServerMethod <TRequest, TResponse> fun = (fRequest, fResponse, fContext) => { return(interceptor.ServerStreamingServerHandler(fRequest, fResponse, fContext, continuation)); }; return(fun); }
public string AddServerStreamingMethod <TRequest, TResponse>(ServerStreamingServerMethod <TRequest, TResponse> callHandler, string?methodName = null) where TRequest : class, IMessage, new() where TResponse : class, IMessage, new() { var method = CreateMethod <TRequest, TResponse>(MethodType.ServerStreaming, methodName ?? Guid.NewGuid().ToString()); AddServiceCore(c => { c.AddServerStreamingMethod(method, new List <object>(), new ServerStreamingServerMethod <DynamicService, TRequest, TResponse>((service, request, stream, context) => callHandler(request, stream, context))); }); return(method.FullName); }
public override async Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { if (!(await IsJwtValid(context.GetAccessToken(), _logger))) { context.Status = new Status(StatusCode.Unauthenticated, "Invalid token"); return; } await continuation(request, responseStream, context); }
public override async Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { try { await continuation(request, responseStream, context); } catch (TaskCanceledException) { // Ignore this exception } }
/// <summary> /// Creates a new instance of <see cref="ServerStreamingServerMethodInvoker{TService, TRequest, TResponse}"/>. /// </summary> /// <param name="invoker">The server streaming method to invoke.</param> /// <param name="method">The description of the gRPC method.</param> /// <param name="options">The options used to execute the method.</param> /// <param name="serviceActivator">The service activator used to create service instances.</param> public ServerStreamingServerMethodInvoker( ServerStreamingServerMethod <TService, TRequest, TResponse> invoker, Method <TRequest, TResponse> method, MethodOptions options, IGrpcServiceActivator <TService> serviceActivator) : base(method, options, serviceActivator) { _invoker = invoker; if (Options.HasInterceptors) { var interceptorPipeline = new InterceptorPipelineBuilder <TRequest, TResponse>(Options.Interceptors); _pipelineInvoker = interceptorPipeline.ServerStreamingPipeline(ResolvedInterceptorInvoker); } }
public string AddServerStreamingMethod <TService, TRequest, TResponse>(ServerStreamingServerMethod <TRequest, TResponse> callHandler, string methodName = null) where TService : class where TRequest : class, IMessage, new() where TResponse : class, IMessage, new() { var method = CreateMethod <TService, TRequest, TResponse>(MethodType.ServerStreaming, methodName ?? Guid.NewGuid().ToString()); Mock <IGrpcMethodModelFactory <TService> > mockFactory = new Mock <IGrpcMethodModelFactory <TService> >(); mockFactory.Setup(m => m.CreateServerStreamingModel(method)).Returns(() => CreateModel(new ServerStreamingServerMethod <TService, TRequest, TResponse>((service, request, stream, context) => callHandler(request, stream, context)))); AddServiceCore((binder, _) => binder.AddMethod(method, (ServerStreamingServerMethod <TRequest, TResponse>)null), mockFactory.Object); return(method.FullName); }
public Method <TRequest, TResponse> AddServerStreamingMethod <TRequest, TResponse>(ServerStreamingServerMethod <TRequest, TResponse> callHandler, MethodDescriptor methodDescriptor) where TRequest : class, IMessage, new() where TResponse : class, IMessage, new() { var method = CreateMethod <TRequest, TResponse>(MethodType.ServerStreaming, methodDescriptor.Name); AddServiceCore(c => { var serverStreamingMethod = new ServerStreamingServerMethod <DynamicService, TRequest, TResponse>((service, request, stream, context) => callHandler(request, stream, context)); var binder = CreateHttpApiBinder <TRequest, TResponse>(methodDescriptor, c, new DynamicServiceInvokerResolver(serverStreamingMethod)); binder.AddMethod(method, callHandler); }); return(method); }
public override async Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { try { await base.ServerStreamingServerHandler(request, responseStream, context, continuation).ConfigureAwait(false); } catch (Exception ex) { _interceptor.OnError(new ServerCallInterceptorContext(context), ex); throw; } }
public override Task ServerStreamingServerHandler <TRequest, TResponse>( TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { LogCall <TRequest, TResponse>(MethodType.ServerStreaming, context); var standIn = new StreamWriterLogger <TResponse>(responseStream, _logger); ServerStreamingServerMethod <TRequest, TResponse> test = new ServerStreamingServerMethod <TRequest, TResponse>(async(request, responseStream, context) => { _logger.LogWarning("now, this is magical OwO"); await continuation(request, standIn, context); }); return(base.ServerStreamingServerHandler(request, standIn, context, test)); }
public ServerStreamingServerCallHandler( Method <TRequest, TResponse> method, ServerStreamingServerMethod <TService, TRequest, TResponse> invoker, MethodContext methodContext, ILoggerFactory loggerFactory, IGrpcServiceActivator serviceActivator, IServiceProvider serviceProvider) : base(method, methodContext, loggerFactory, serviceActivator, serviceProvider) { _invoker = invoker; if (MethodContext.HasInterceptors) { var interceptorPipeline = new InterceptorPipelineBuilder <TRequest, TResponse>(MethodContext.Interceptors, ServiceProvider); _pipelineInvoker = interceptorPipeline.ServerStreamingPipeline(ResolvedInterceptorInvoker); } }
public override void AddMethod <TRequest, TResponse>(Method <TRequest, TResponse> method, ServerStreamingServerMethod <TRequest, TResponse> handler) { _server._methodHandlers.Add(method.FullName, async ctx => { try { var request = await ctx.GetMessageReader(method.RequestMarshaller).ReadNextMessage() .ConfigureAwait(false); await handler( request, ctx.CreateResponseStream(method.ResponseMarshaller), ctx.CallContext).ConfigureAwait(false); ctx.Success(); } catch (Exception ex) { ctx.Error(ex); } }); }
public override async Task ServerStreamingServerHandler <TRequest, TResponse>(TRequest request, IServerStreamWriter <TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod <TRequest, TResponse> continuation) { (string, string)tuple = InterceptCallContext(context); var watch = Stopwatch.StartNew(); await continuation(request, responseStream, context); Logger.Log.GrpcTrace(m_loggingContext, string.Format(RespondedLogFormat, tuple.Item1, tuple.Item2, watch.ElapsedMilliseconds)); }
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 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; }); }