Example #1
0
        public static IDrexBuilder AddConsul(this IDrexBuilder 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));
        }
Example #2
0
        public static IDrexBuilder AddRabbitMq <TContext>(this IDrexBuilder 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)));
        }
Example #3
0
        public static IDrexBuilder AddRabbitMq(this IDrexBuilder builder, string sectionName = SectionName,
                                               Func <IRabbitMqPluginsRegistry, IRabbitMqPluginsRegistry> plugins = 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));
            }

            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 connection = new ConnectionFactory
            {
                Port        = options.Port,
                VirtualHost = options.VirtualHost,
                UserName    = options.Username,
                Password    = options.Password,
                RequestedConnectionTimeout = options.RequestedConnectionTimeout,
                SocketReadTimeout          = options.SocketReadTimeout,
                SocketWriteTimeout         = options.SocketWriteTimeout,
                RequestedChannelMax        = options.RequestedChannelMax,
                RequestedFrameMax          = options.RequestedFrameMax,
                RequestedHeartbeat         = options.RequestedHeartbeat,
                UseBackgroundThreadsForIO  = options.UseBackgroundThreadsForIO,
                DispatchConsumersAsync     = true,
                Ssl = options.Ssl is null
                    ? new SslOption()
                    : new SslOption(options.Ssl.ServerName, options.Ssl.CertificatePath, options.Ssl.Enabled)
            }.CreateConnection(options.HostNames.ToList(), options.ConnectionName);
Example #4
0
        public static IDrexBuilder AddMetrics(this IDrexBuilder builder,
                                              string metricsSectionName = MetricsSectionName, string appSectionName = AppSectionName)
        {
            if (string.IsNullOrWhiteSpace(metricsSectionName))
            {
                metricsSectionName = MetricsSectionName;
            }

            if (string.IsNullOrWhiteSpace(appSectionName))
            {
                appSectionName = AppSectionName;
            }

            var metricsOptions = builder.GetOptions <MetricsOptions>(metricsSectionName);
            var appOptions     = builder.GetOptions <AppOptions>(appSectionName);

            return(builder.AddMetrics(metricsOptions, appOptions));
        }
Example #5
0
        public static IDrexBuilder AddMongo(this IDrexBuilder builder, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

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

            return(builder.AddMongo(mongoOptions));
        }
Example #6
0
        public static IDrexBuilder AddRedis(this IDrexBuilder builder, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

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

            return(builder.AddRedis(options));
        }
Example #7
0
        public static IDrexBuilder AddJwt(this IDrexBuilder 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));
        }
Example #8
0
        public static IDrexBuilder AddSqlServer <TMyDbContext>(this IDrexBuilder builder,
                                                               string sectionName = SectionName)
            where TMyDbContext : DbContext
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

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

            return(builder.AddSqlServer <TMyDbContext>(mongoOptions));
        }
Example #9
0
        public static IDrexBuilder AddMetrics(this IDrexBuilder builder,
                                              Func <IMetricsOptionsBuilder, IMetricsOptionsBuilder> buildOptions, string appSectionName = AppSectionName)
        {
            if (string.IsNullOrWhiteSpace(appSectionName))
            {
                appSectionName = AppSectionName;
            }

            var metricsOptions = buildOptions(new MetricsOptionsBuilder()).Build();
            var appOptions     = builder.GetOptions <AppOptions>(appSectionName);

            return(builder.AddMetrics(metricsOptions, appOptions));
        }
Example #10
0
        public static IDrexBuilder AddServiceClient <T>(this IDrexBuilder 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)));
        }
Example #11
0
        public static IDrexBuilder AddCertificateAuthentication(this IDrexBuilder 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 {})
Example #12
0
        public static IDrexBuilder AddHttpClient(this IDrexBuilder builder,
                                                 string clientName = "Drex",
                                                 IEnumerable <string> maskedRequestUrlParts = null, string sectionName = SectionName)
        {
            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 {})
Example #13
0
        public static IDrexBuilder AddWebApi(this IDrexBuilder builder,
                                             Action <IMvcCoreBuilder> configureMvc = null,
                                             IJsonSerializer jsonSerializer        = null, string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

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

            if (jsonSerializer is null)
            {
                var factory = new JsonSerializerFactory(new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Converters       = { new StringEnumConverter(true) }
                });
                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);

            // This method gets called by the runtime. Use this method to add services to the container.
            builder.Services.Scan(s =>
                                  s.FromAssemblies(AppDomain.CurrentDomain.GetAssemblies())
                                  .AddClasses(c => c.AssignableTo(typeof(IRequestHandler <,>)))
                                  .AsImplementedInterfaces()
                                  .WithTransientLifetime());

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

            return(builder);
        }