Пример #1
0
        private static object CreateGrpcMethodForCqrsChannel(CqrsChannelInfo info)
        {
            var grpcMethodFnc = typeof(GrpcMethodFactoryUtil).GetMethod(nameof(GrpcMethodFactoryUtil.CreateGrpcMethod)).MakeGenericMethod(info.ChReqType, info.ChRspEnvType);
            var grpcMethod    = grpcMethodFnc.Invoke(null, new object[] { info.ServiceName, info.MethodName });

            return(grpcMethod);
        }
Пример #2
0
        public static object CreateServerMethodGeneric(
            Container container,
            string serverId,
            CqrsChannelInfo info,
            IMapper mapper,
            int timeoutMs)
        {
            var method = typeof(GrpcServerMethodFactoryUtil).GetMethods(BindingFlags.Static | BindingFlags.Public)
                         .First(x => x.Name == nameof(CreateServerMethod)).MakeGenericMethod(info.ReqType, info.RspType, info.ChReqType, info.ChRspType, info.ChRspEnvType);
            var grpcMethod = method.Invoke(null, new object[] { container, serverId, info, mapper, timeoutMs });

            return(grpcMethod);
        }
Пример #3
0
        public static UnaryServerMethod <GrpcService, TChReq, TChRspEnvelope> CreateServerMethod <TReq, TRsp, TChReq, TChRsp, TChRspEnvelope>(
            Container container,
            string serverId,
            CqrsChannelInfo info,
            IMapper mapper,
            int timeoutMs
            )
        {
            var method = new UnaryServerMethod <GrpcService, TChReq, TChRspEnvelope>(async(svc, chReq, ctx) => {
                // add timeout to cancellation token
                var cancellationToken = ctx.CancellationToken.AddTimeout(timeoutMs);

                // handle request
                var rsp = await GrpcChannelHandlerUtil.HandleChannelRequest <TReq, TRsp, TChReq, TChRsp, TChRspEnvelope>(
                    container, mapper, ctx, serverId, info, chReq, cancellationToken);
                return(rsp);
            });

            return(method);
        }
Пример #4
0
        private void CreateGrpcMethodForCqrsChannel <TRequest, TResponse, TChRequest, TChResponse, TChResponseEnvelope>(GrpcServer server, string serverId, CqrsChannelInfo info)
            where TRequest : class
            where TResponse : class
            where TChRequest : class
            where TChResponse : class
            where TChResponseEnvelope : class
        {
            var builder = ServerServiceDefinition.CreateBuilder();
            var method  = GrpcMethodFactoryUtil.CreateGrpcMethod <TChRequest, TChResponseEnvelope>(info.ServiceName, info.MethodName);
            var handler = new UnaryServerMethod <TChRequest, TChResponseEnvelope>(async(chReq, ctx) => {
                // timeout
                var cancellationToken = ctx.CancellationToken.AddTimeout(_configuration.TimeoutMs);

                // execute in scope
                using (Scope scope = AsyncScopedLifestyle.BeginScope(_container))
                {
                    // execute method
                    var chRspEnv = await GrpcChannelHandlerUtil.HandleChannelRequest <TRequest, TResponse, TChRequest, TChResponse, TChResponseEnvelope>(
                        scope.Container,
                        _mapper,
                        ctx,
                        serverId,
                        info,
                        chReq,
                        cancellationToken);
                    return(chRspEnv);
                }
            });

            // add method to server
            builder.AddMethod(method, handler);
            var service = builder.Build();

            server.Services.Add(service);
        }