Exemplo n.º 1
0
        public GrpcCqrsClient(GrpcCqrsClientConfiguration configuration,
                              ILogger <GrpcCqrsClient> logger,
                              IGrpcClientAspect clientAspect = null)
        {
            _configuration = configuration;
            _logger        = logger;
            _clientAspect  = clientAspect;

            // client id definition
            Id = !string.IsNullOrWhiteSpace(_configuration.ClientId) ? _configuration.ClientId : Assembly.GetEntryAssembly().FullName.Split(',')[0];

            // resolve cqrs from assemblies
            var cqrs = configuration.ContractsAssemblies.SelectMany(CqrsInfoResolverUtil.GetCqrsDefinitions).ToList();

            _cqrsAdapter = new CqrsContractsAdapter(cqrs, configuration.ServiceNamePrefix);

            // create grpc invokation methods
            _grpcMethods = _cqrsAdapter.ToCqrsChannelInfo().ToDictionary(
                x => x.ChReqType,
                x => CreateGrpcMethodForCqrsChannel(x));

            // create mapper
            _mapper = _cqrsAdapter.CreateMapper();

            // create client
            var ch = new Channel(configuration.Url, configuration.Port, ChannelCredentials.Insecure);

            _invoker = new DefaultCallInvoker(ch);
        }
Exemplo n.º 2
0
        public async Task <GrpcResponseEnvelope <TResponse> > Execute <TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException();
            }

            // timeout
            cancellationToken = cancellationToken.AddTimeout(_configuration.TimeoutMs);

            // ch info
            var chInfo = _cqrsAdapter.ToCqrsChannelInfo().FirstOrDefault(x => x.ReqType == request.GetType() && x.RspType == typeof(TResponse));

            if (chInfo == null)
            {
                throw new ArgumentException("Invaid request type.");
            }

            // calll
            var callMethod = GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
                             .First(x => x.Name == nameof(GrpcCqrsClient.CallUnaryMethodAsync))
                             .MakeGenericMethod(chInfo.ReqType, chInfo.RspType, chInfo.ChReqType, chInfo.ChRspType, chInfo.ChRspEnvType);
            var execTask = callMethod.Invoke(this, new object[] { request, default(CancellationToken) }) as Task <GrpcResponseEnvelope <TResponse> >;
            var result   = await execTask;

            return(result);
        }
Exemplo n.º 3
0
        public void OnServiceMethodDiscovery(ServiceMethodProviderContext <GrpcService> context)
        {
            // mapper
            var mapperValidator = _cfg.MapperValidator != null?Activator.CreateInstance(_cfg.MapperValidator) as IPropertyMapValidator : null;

            var mapper = _cqrs.CreateMapper(mapperValidator);

            // set server id for logging
            var serverId = !string.IsNullOrWhiteSpace(_cfg.ServerId) ? _cfg.ServerId : Assembly.GetEntryAssembly().FullName.Split(',')[0];

            // create methos
            var methodAddGeneric = GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(x => x.Name == nameof(AddMethod));

            _cqrs.ToCqrsChannelInfo().ToList().ForEach(x => {
                var method       = GrpcMethodFactoryUtil.CreateGrpcMethodGeneric(x);
                var invokeMethod = GrpcServerMethodFactoryUtil.CreateServerMethodGeneric(_container, serverId, x, mapper, _cfg.TimeoutMs);
                var methodAdd    = methodAddGeneric.MakeGenericMethod(x.ChReqType, x.ChRspEnvType);
                methodAdd.Invoke(this, new object[] { context, method, invokeMethod });
            });
        }
Exemplo n.º 4
0
        public GrpcCqrsServer(GrpcCqrsServerConfiguration configuration, Container container)
        {
            _container     = container;
            _configuration = configuration;

            // resolve cqrs from assemblies
            var cqrs = configuration.ContractsAssemblies.SelectMany(CqrsInfoResolverUtil.GetCqrsDefinitions).ToList();

            if (configuration.RegisteredOnly)
            {
                var allTypes = container.GetCurrentRegistrations().Select(x => x.Registration.ImplementationType).SelectMany(x => x.GetInterfaces());
                cqrs = cqrs.Where(x => allTypes.Contains(x.GetHandlerType())).ToList();
            }
            var cqrsApater = new CqrsContractsAdapter(cqrs, _configuration.ServiceNamePrefix);

            // types map
            _cqrsChannelInfos = cqrsApater.ToCqrsChannelInfo();

            // set server id for logging
            var serverId = !string.IsNullOrWhiteSpace(configuration.ServerId) ? configuration.ServerId : Assembly.GetEntryAssembly().FullName.Split(',')[0];

            // create server
            var createServiceMethod = GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
                                      .First(x => x.Name == nameof(GrpcCqrsServer.CreateGrpcMethodForCqrsChannel));

            _server = new GrpcServer()
            {
                Ports = { { configuration.Url, configuration.Port, ServerCredentials.Insecure } },
            };
            _cqrsChannelInfos.ForEach(x => {
                var create = createServiceMethod.MakeGenericMethod(x.ReqType, x.RspType, x.ChReqType, x.ChRspType, x.ChRspEnvType);
                create.Invoke(this, new object[] { _server, serverId, x });
            });

            // create mapper
            var mapperValidator = configuration.MapperValidator != null?Activator.CreateInstance(configuration.MapperValidator) as IPropertyMapValidator : null;

            _mapper = cqrsApater.CreateMapper(mapperValidator);
        }