public static GrpcCqrsServerConfiguration ToConfiguration(this GrpcCqrsServerRawConfiguration raw)
        {
            var mapType         = !string.IsNullOrWhiteSpace(raw?.MapperValidator) ? Type.GetType(raw.MapperValidator) : null;
            var mapperValidator = mapType.GetAllInterfaces().Any(x => x == typeof(IPropertyMapValidator)) ? mapType : null;

            var cfg = new GrpcCqrsServerConfiguration
            {
                Url                 = raw.Url,
                Port                = raw.Port,
                TimeoutMs           = raw.TimeoutMs,
                ServiceNamePrefix   = raw.ServiceNamePrefix,
                ContractsAssemblies = raw.ContractsAssemblies.ToAssemblies(),
                RegisteredOnly      = raw.RegisteredOnly,
                MapperValidator     = mapperValidator,
                ServerId            = raw.ServerId
            };

            return(cfg);
        }
Ejemplo n.º 2
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);
        }