Example #1
0
        public IRpcHostBuilder AddUnaryMethod <TService, TRequest, TResponse>(
            Func <TService, TRequest, CancellationToken, Task <TResponse> > handler,
            string serviceName,
            string methodName
            )
            where TService : class, IRpcService
            where TRequest : class
            where TResponse : class
        {
            this._builder.AddMethod(
                MethodDefinitionGenerator.CreateMethodDefinition <TRequest, TResponse>(MethodType.Unary, serviceName, methodName, this._serializer),
                (request, context) => {
                using (var scope = this._serviceProvider.CreateScope())
                {
                    var service = scope.ServiceProvider.GetServices <TService>().First(s => !s.GetType().Name.EndsWith("GrpcClientProxy", StringComparison.OrdinalIgnoreCase));
                    if (service is RpcServiceBase baseService)
                    {
                        baseService.Context = context;
                    }
                    return(handler(service, request, context.CancellationToken));
                }
            }
                );

            return(this);
        }
Example #2
0
 public GrpcHostBuilder <TService> AddUnaryMethod <TRequest, TResponse>(
     Func <TService, TRequest, CancellationToken, Task <TResponse> > handler,
     string serviceName,
     string methodName
     )
     where TRequest : class
     where TResponse : class
 {
     _builder.AddMethod(
         MethodDefinitionGenerator.CreateMethodDefinition <TRequest, TResponse>(MethodType.Unary, serviceName, methodName, _serializer),
         async(request, context) =>
     {
         using (var scope = _appServices.CreateScope())
         {
             var service     = scope.ServiceProvider.GetRequiredService <TService>();
             var baseService = service as GrpcServiceBase;
             if (baseService != null)
             {
                 baseService.Context = context;
             }
             return(await handler(service, request, context.CancellationToken).ConfigureAwait(false));
         }
     }
         );
     return(this);
 }
 private GrpcCore.Method <TRequest, TResponse> GetMethodDefinition <TRequest, TResponse>(GrpcCore.MethodType methodType, string serviceName, string methodName)
     where TRequest : class
     where TResponse : class
 {
     return(MethodDefinitionGenerator.CreateMethodDefinition <TRequest, TResponse>(methodType, serviceName, methodName, this._serializer));
 }