Beispiel #1
0
        public static IBioWorldBuilder AddConsul(this IBioWorldBuilder builder, string sectionName = SectionName,
                                                 string httpClientSectionName = "httpClient")
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var consulOptions     = builder.GetOptions <ConsulOptions>(sectionName);
            var httpClientOptions = builder.GetOptions <HttpClientOptions>(httpClientSectionName);

            return(builder.AddConsul(consulOptions, httpClientOptions));
        }
Beispiel #2
0
        public static IBioWorldBuilder AddRabbitMq <TContext>(this IBioWorldBuilder builder, string sectionName = SectionName,
                                                              string redisSectionName = "redis", Func <IRabbitMqPluginRegister, IRabbitMqPluginRegister> plugins = null)
            where TContext : class, new()
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var options      = builder.GetOptions <RabbitMqOptions>(sectionName);
            var redisOptions = builder.GetOptions <RedisOptions>(redisSectionName);

            return(builder.AddRabbitMq <TContext>(options, plugins, b => b.AddRedis(redisOptions)));
        }
Beispiel #3
0
        public static IBioWorldBuilder AddRedis(this IBioWorldBuilder builder, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var options = builder.GetOptions <RedisOptions>(sectionName);

            return(builder.AddRedis(options));
        }
Beispiel #4
0
        public static IBioWorldBuilder AddJaeger(this IBioWorldBuilder builder, string sectionName = SectionName,
                                                 Action <IOpenTracingBuilder> openTracingBuilder   = null)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var options = builder.GetOptions <JaegerOptions>(sectionName);

            return(builder.AddJaeger(options, sectionName, openTracingBuilder));
        }
Beispiel #5
0
        public static IBioWorldBuilder AddMongo(this IBioWorldBuilder builder, string sectionName = SectionName,
                                                Type seederType = null, bool registerConventions = true)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var mongoOptions = builder.GetOptions <MongoDbOptions>(sectionName);

            return(builder.AddMongo(mongoOptions, seederType, registerConventions));
        }
Beispiel #6
0
        public static IBioWorldBuilder AddJwt(this IBioWorldBuilder builder, string sectionName = SectionName,
                                              Action <JwtBearerOptions> optionsFactory          = null)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var options = builder.GetOptions <JwtOptions>(sectionName);

            return(builder.AddJwt(options, optionsFactory));
        }
Beispiel #7
0
        public static IBioWorldBuilder AddServiceClient <T>(this IBioWorldBuilder builder, string serviceName,
                                                            string sectionName           = SectionName, string consulSectionName = "consul", string fabioSectionName = "fabio",
                                                            string httpClientSectionName = "httpClient")
            where T : class
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var restEaseOptions = builder.GetOptions <RestEaseOptions>(sectionName);

            return(builder.AddServiceClient <T>(serviceName, restEaseOptions,
                                                b => b.AddFabio(fabioSectionName, consulSectionName, httpClientSectionName)));
        }
Beispiel #8
0
        public static IBioWorldBuilder AddCertificateAuthentication(this IBioWorldBuilder builder,
                                                                    string sectionName = SectionName, Type permissionValidatorType = null)
        {
            var options = builder.GetOptions <SecurityOptions>(sectionName);

            builder.Services.AddSingleton(options);
            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            if (options.Certificate is null || !options.Certificate.Enabled)
            {
                return(builder);
            }

            if (permissionValidatorType is { })
Beispiel #9
0
        public static IBioWorldBuilder AddHttpClient(this IBioWorldBuilder builder, string clientName = "bioworld",
                                                     IEnumerable <string> maskedRequestUrlParts       = null, string sectionName = SectionName,
                                                     Action <IHttpClientBuilder> httpClientBuilder    = null)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            if (string.IsNullOrWhiteSpace(clientName))
            {
                throw new ArgumentException("HTTP client name cannot be empty.", nameof(clientName));
            }

            var options = builder.GetOptions <HttpClientOptions>(sectionName);

            if (maskedRequestUrlParts is { } && options.RequestMasking is { })
Beispiel #10
0
        public static IBioWorldBuilder AddMessageOutbox(this IBioWorldBuilder builder,
                                                        Action <IMessageOutboxConfigurator> configure = null, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            var options = builder.GetOptions <OutboxOptions>(sectionName);

            builder.Services.AddSingleton(options);
            var configurator = new MessageOutboxConfigurator(builder, options);

            if (configure is null)
            {
                configurator.AddInMemory();
            }
            else
            {
                configure(configurator);
            }

            if (!options.Enabled)
            {
                return(builder);
            }

            builder.Services.AddHostedService <OutboxProcessor>();

            return(builder);
        }
Beispiel #11
0
        public static IBioWorldBuilder AddWebApi(this IBioWorldBuilder builder,
                                                 Action <IMvcCoreBuilder> configureMvc = null,
                                                 IJsonSerializer jsonSerializer        = null, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            if (!builder.TryRegister(RegisterName))
            {
                return(builder);
            }

            if (jsonSerializer is null)
            {
                var factory = new Open.Serialization.Json.Newtonsoft.JsonSerializerFactory(new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Converters       = { new StringEnumConverter() }
                });
                jsonSerializer = factory.GetSerializer();
            }

            if (jsonSerializer.GetType().Namespace?.Contains("Newtonsoft") == true)
            {
                builder.Services.Configure <KestrelServerOptions>(o => o.AllowSynchronousIO = true);
                builder.Services.Configure <IISServerOptions>(o => o.AllowSynchronousIO     = true);
            }

            builder.Services.AddSingleton(jsonSerializer);
            builder.Services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            builder.Services.AddSingleton(new WebApiEndpointDefinitions());
            var options = builder.GetOptions <WebApiOptions>(sectionName);

            builder.Services.AddSingleton(options);
            _bindRequestFromRoute = options.BindRequestFromRoute;

            var mvcCoreBuilder = builder.Services.AddLogging().AddMvcCore();

            mvcCoreBuilder.AddMvcOptions(o =>
            {
                o.OutputFormatters.Clear();
                o.OutputFormatters.Add(new JsonOutputFormatter(jsonSerializer));
                o.InputFormatters.Clear();
                o.InputFormatters.Add(new JsonInputFormatter(jsonSerializer));
            })
            .AddDataAnnotations()
            .AddApiExplorer()
            .AddAuthorization();

            configureMvc?.Invoke(mvcCoreBuilder);
            builder.Services.Scan(s =>
                                  s.FromAssemblies(AppDomain.CurrentDomain.GetAssemblies()).AddClasses(c =>
                                                                                                       c.AssignableTo(typeof(IRequestHandler <,>)).WithoutAttribute(typeof(DecoratorAttribute)))
                                  .AsImplementedInterfaces().WithTransientLifetime());

            builder.Services.AddTransient <IRequestDispatcher, RequestDispatcher>();

            if (builder.Services.All(s => s.ServiceType != typeof(IExceptionToResponseMapper)))
            {
                builder.Services.AddTransient <IExceptionToResponseMapper, EmptyExceptionToResponseMapper>();
            }

            return(builder);
        }
Beispiel #12
0
        public static IBioWorldBuilder AddRabbitMq(this IBioWorldBuilder builder, string sectionName = SectionName,
                                                   Func <IRabbitMqPluginsRegistry, IRabbitMqPluginsRegistry> plugins = null,
                                                   Action <ConnectionFactory> connectionFactoryConfigurator          = null)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            var options = builder.GetOptions <RabbitMqOptions>(sectionName);

            builder.Services.AddSingleton(options);
            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            if (options.HostNames is null || !options.HostNames.Any())
            {
                throw new ArgumentException("RabbitMQ hostnames are not specified.", nameof(options.HostNames));
            }

            ILogger <IRabbitMqClient> logger;

            using (var serviceProvider = builder.Services.BuildServiceProvider())
            {
                logger = serviceProvider.GetService <ILogger <IRabbitMqClient> >();
            }

            builder.Services.AddSingleton <IContextProvider, ContextProvider>();
            builder.Services.AddSingleton <ICorrelationContextAccessor>(new CorrelationContextAccessor());
            builder.Services.AddSingleton <IMessagePropertiesAccessor>(new MessagePropertiesAccessor());
            builder.Services.AddSingleton <IConventionsBuilder, ConventionsBuilder>();
            builder.Services.AddSingleton <IConventionsProvider, ConventionsProvider>();
            builder.Services.AddSingleton <IConventionsRegistry, ConventionsRegistry>();
            builder.Services.AddSingleton <IRabbitMqSerializer, NewtonsoftJsonRabbitMqSerializer>();
            builder.Services.AddSingleton <IRabbitMqClient, RabbitMqClient>();
            builder.Services.AddSingleton <IBusPublisher, RabbitMqPublisher>();
            builder.Services.AddSingleton <IBusSubscriber, RabbitMqSubscriber>();
            builder.Services.AddTransient <RabbitMqExchangeInitializer>();
            builder.Services.AddHostedService <RabbitMqHostedService>();
            builder.AddInitializer <RabbitMqExchangeInitializer>();

            var pluginsRegistry = new RabbitMqPluginsRegistry();

            builder.Services.AddSingleton <IRabbitMqPluginsRegistryAccessor>(pluginsRegistry);
            builder.Services.AddSingleton <IRabbitMqPluginsExecutor, RabbitMqPluginsExecutor>();
            plugins?.Invoke(pluginsRegistry);

            var connectionFactory = new ConnectionFactory
            {
                Port                         = options.Port,
                VirtualHost                  = options.VirtualHost,
                UserName                     = options.Username,
                Password                     = options.Password,
                RequestedHeartbeat           = options.RequestedHeartbeat,
                RequestedConnectionTimeout   = options.RequestedConnectionTimeout,
                SocketReadTimeout            = options.SocketReadTimeout,
                SocketWriteTimeout           = options.SocketWriteTimeout,
                RequestedChannelMax          = options.RequestedChannelMax,
                RequestedFrameMax            = options.RequestedFrameMax,
                UseBackgroundThreadsForIO    = options.UseBackgroundThreadsForIO,
                DispatchConsumersAsync       = true,
                ContinuationTimeout          = options.ContinuationTimeout,
                HandshakeContinuationTimeout = options.HandshakeContinuationTimeout,
                NetworkRecoveryInterval      = options.NetworkRecoveryInterval,
                Ssl = options.Ssl is null
                    ? new SslOption()
                    : new SslOption(options.Ssl.ServerName, options.Ssl.CertificatePath, options.Ssl.Enabled)
            };

            ConfigureSsl(connectionFactory, options, logger);
            connectionFactoryConfigurator?.Invoke(connectionFactory);

            logger.LogDebug($"Connecting to RabbitMQ: '{string.Join(", ", options.HostNames)}'...");
            var connection = connectionFactory.CreateConnection(options.HostNames.ToList(), options.ConnectionName);

            logger.LogDebug($"Connected to RabbitMQ: '{string.Join(", ", options.HostNames)}'.");
            builder.Services.AddSingleton(connection);

            ((IRabbitMqPluginsRegistryAccessor)pluginsRegistry).Get().ToList().ForEach(p =>
                                                                                       builder.Services.AddTransient(p.PluginType));

            return(builder);
        }