예제 #1
0
 private static IDrexBuilder AddWebApiSwaggerDocs(this IDrexBuilder builder,
                                                  Action <IDrexBuilder> registerSwagger)
 {
     registerSwagger(builder);
     builder.Services.AddSwaggerGen(c => c.DocumentFilter <WebApiDocumentFilter>());
     return(builder);
 }
예제 #2
0
        public static IDrexBuilder AddRedis(this IDrexBuilder builder,
                                            Func <IRedisOptionsBuilder, IRedisOptionsBuilder> buildOptions)
        {
            var options = buildOptions(new RedisOptionsBuilder()).Build();

            return(builder.AddRedis(options));
        }
예제 #3
0
        public static IDrexBuilder AddMongo(this IDrexBuilder builder, Func <IMongoDbOptionsBuilder,
                                                                             IMongoDbOptionsBuilder> buildOptions)
        {
            var mongoOptions = buildOptions(new MongoDbOptionsBuilder()).Build();

            return(builder.AddMongo(mongoOptions));
        }
예제 #4
0
        public static IDrexBuilder AddConsul(this IDrexBuilder builder, ConsulOptions options,
                                             HttpClientOptions httpClientOptions)
        {
            builder.Services.AddSingleton(options);
            if (!options.Enabled || !builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            if (httpClientOptions.Type?.ToLowerInvariant() == "consul")
            {
                builder.Services.AddTransient <ConsulServiceDiscoveryMessageHandler>();
                builder.Services.AddHttpClient <IConsulHttpClient, ConsulHttpClient>("consul-http")
                .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>();
                builder.RemoveHttpClient();
                builder.Services.AddHttpClient <IHttpClient, ConsulHttpClient>("consul")
                .AddHttpMessageHandler <ConsulServiceDiscoveryMessageHandler>();
            }

            builder.Services.AddTransient <IConsulServicesRegistry, ConsulServicesRegistry>();
            var registration = builder.CreateConsulAgentRegistration(options);

            if (registration is null)
            {
                return(builder);
            }

            builder.Services.AddSingleton(registration);

            return(builder);
        }
예제 #5
0
 public static void AddConsulHttpClient(this IDrexBuilder builder, string clientName, string serviceName)
 {
     builder.Services.AddHttpClient <IHttpClient, ConsulHttpClient>(clientName)
     .AddHttpMessageHandler(c => new ConsulServiceDiscoveryMessageHandler(
                                c.GetService <IConsulServicesRegistry>(),
                                c.GetService <ConsulOptions>(), serviceName, true));
 }
예제 #6
0
        public static IDrexBuilder AddMongo(this IDrexBuilder builder, MongoDbOptions mongoOptions)
        {
            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            builder.Services.AddSingleton(mongoOptions);
            builder.Services.AddSingleton <IMongoClient>(sp =>
            {
                var options = sp.GetService <MongoDbOptions>();
                return(new MongoClient(options.ConnectionString));
            });
            builder.Services.AddTransient(sp =>
            {
                var options = sp.GetService <MongoDbOptions>();
                var client  = sp.GetService <IMongoClient>();
                return(client.GetDatabase(options.Database));
            });
            builder.Services.AddTransient <IMongoDbInitializer, MongoDbInitializer>();
            builder.Services.AddTransient <IMongoSessionFactory, MongoSessionFactory>();

            builder.AddInitializer <IMongoDbInitializer>();

            return(builder);
        }
예제 #7
0
        public static IDrexBuilder AddConsul(this IDrexBuilder builder,
                                             Func <IConsulOptionsBuilder, IConsulOptionsBuilder> buildOptions, HttpClientOptions httpClientOptions)
        {
            var options = buildOptions(new ConsulOptionsBuilder()).Build();

            return(builder.AddConsul(options, httpClientOptions));
        }
예제 #8
0
        public static IDrexBuilder AddSwaggerDocs(this IDrexBuilder builder,
                                                  Func <ISwaggerOptionsBuilder, ISwaggerOptionsBuilder> buildOptions)
        {
            var options = buildOptions(new SwaggerOptionsBuilder()).Build();

            return(builder.AddSwaggerDocs(options));
        }
예제 #9
0
        private static IDrexBuilder AddServiceClient <T>(this IDrexBuilder builder, string serviceName,
                                                         RestEaseOptions options, Action <IDrexBuilder> registerFabio)
            where T : class
        {
            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            var clientName = typeof(T).ToString();

            switch (options.LoadBalancer?.ToLowerInvariant())
            {
            case "consul":
                builder.AddConsulHttpClient(clientName, serviceName);
                break;

            case "fabio":
                builder.AddFabioHttpClient(clientName, serviceName);
                break;

            default:
                ConfigureDefaultClient(builder.Services, clientName, serviceName, options);
                break;
            }

            ConfigureForwarder <T>(builder.Services, clientName);

            registerFabio(builder);

            return(builder);
        }
예제 #10
0
        public static IDrexBuilder AddSwaggerDocs(this IDrexBuilder builder, SwaggerOptions options)
        {
            if (!options.Enabled || !builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            builder.Services.AddSingleton(options);
            builder.Services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(options.Name, new OpenApiInfo {
                    Title = options.Title, Version = options.Version
                });
                if (options.IncludeSecurity)
                {
                    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                    {
                        Description =
                            "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                        Name = "Authorization",
                        In   = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey
                    });
                }
            });

            return(builder);
        }
예제 #11
0
 public static IDrexBuilder AddRabbitMq <TContext>(this IDrexBuilder builder, RabbitMqOptions options,
                                                   Func <IRabbitMqPluginRegister, IRabbitMqPluginRegister> plugins = null,
                                                   RedisOptions redisOptions = null)
     where TContext : class, new()
 {
     return(builder.AddRabbitMq <TContext>(options, plugins, b => b.AddRedis(redisOptions ?? new RedisOptions())));
 }
예제 #12
0
        public static IDrexBuilder AddExceptionToMessageMapper <T>(this IDrexBuilder builder)
            where T : class, IExceptionToMessageMapper
        {
            builder.Services.AddTransient <IExceptionToMessageMapper, T>();

            return(builder);
        }
예제 #13
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);
예제 #14
0
        public static IDrexBuilder AddErrorHandler <T>(this IDrexBuilder builder)
            where T : class, IExceptionToResponseMapper
        {
            builder.Services.AddTransient <ErrorHandlerMiddleware>();
            builder.Services.AddSingleton <IExceptionToResponseMapper, T>();

            return(builder);
        }
예제 #15
0
        public static IDrexBuilder AddSqlServer <TMyDbContext>(this IDrexBuilder builder,
                                                               Func <ISqlServerOptionsBuilder,
                                                                     ISqlServerOptionsBuilder> buildOptions) where TMyDbContext : DbContext
        {
            var mongoOptions = buildOptions(new SqlServerOptionsBuilder()).Build();

            return(builder.AddSqlServer <TMyDbContext>(mongoOptions));
        }
예제 #16
0
 public static IDrexBuilder AddServiceClient <T>(this IDrexBuilder builder, string serviceName,
                                                 RestEaseOptions options, ConsulOptions consulOptions, FabioOptions fabioOptions,
                                                 HttpClientOptions httpClientOptions)
     where T : class
 {
     return(builder.AddServiceClient <T>(serviceName, options,
                                         b => b.AddFabio(fabioOptions, consulOptions, httpClientOptions)));
 }
예제 #17
0
        public static TModel GetOptions <TModel>(this IDrexBuilder builder, string settingsSectionName)
            where TModel : new()
        {
            using var serviceProvider = builder.Services.BuildServiceProvider();
            var configuration = serviceProvider.GetService <IConfiguration>();

            return(configuration.GetOptions <TModel>(settingsSectionName));
        }
예제 #18
0
        public static IDrexBuilder AddWebApiSwaggerDocs(this IDrexBuilder builder,
                                                        string sectionName = SectionName)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                sectionName = SectionName;
            }

            return(builder.AddWebApiSwaggerDocs(b => b.AddSwaggerDocs(sectionName)));
        }
예제 #19
0
        public static IDrexBuilder AddCommandHandlers(this IDrexBuilder builder)
        {
            builder.Services.Scan(s =>
                                  s.FromAssemblies(AppDomain.CurrentDomain.GetAssemblies())
                                  .AddClasses(c => c.AssignableTo(typeof(ICommandHandler <>)))
                                  .AsImplementedInterfaces()
                                  .WithTransientLifetime());

            return(builder);
        }
예제 #20
0
        public static IDrexBuilder AddFabio(this IDrexBuilder builder,
                                            Func <IFabioOptionsBuilder, IFabioOptionsBuilder> buildOptions,
                                            Func <IConsulOptionsBuilder, IConsulOptionsBuilder> buildConsulOptions,
                                            HttpClientOptions httpClientOptions)
        {
            var fabioOptions = buildOptions(new FabioOptionsBuilder()).Build();

            return(builder.AddFabio(fabioOptions, httpClientOptions,
                                    b => b.AddConsul(buildConsulOptions, httpClientOptions)));
        }
예제 #21
0
        public static IDrexBuilder AddDistributedAccessTokenValidator(this IDrexBuilder builder)
        {
            if (!builder.TryRegister(RegistryName))
            {
                return(builder);
            }

            builder.Services.AddSingleton <IAccessTokenService, DistributedAccessTokenService>();

            return(builder);
        }
예제 #22
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));
        }
예제 #23
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));
        }
예제 #24
0
        public static IDrexBuilder AddServiceClient <T>(this IDrexBuilder builder, string serviceName,
                                                        Func <IRestEaseOptionsBuilder, IRestEaseOptionsBuilder> buildOptions,
                                                        Func <IConsulOptionsBuilder, IConsulOptionsBuilder> buildConsulOptions,
                                                        Func <IFabioOptionsBuilder, IFabioOptionsBuilder> buildFabioOptions,
                                                        HttpClientOptions httpClientOptions)
            where T : class
        {
            var options = buildOptions(new RestEaseOptionsBuilder()).Build();

            return(builder.AddServiceClient <T>(serviceName, options,
                                                b => b.AddFabio(buildFabioOptions, buildConsulOptions, httpClientOptions)));
        }
예제 #25
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));
        }
예제 #26
0
        public static IDrexBuilder AddRabbitMq <TContext>(this IDrexBuilder builder,
                                                          Func <IRabbitMqOptionsBuilder, IRabbitMqOptionsBuilder> buildOptions,
                                                          Func <IRabbitMqPluginRegister, IRabbitMqPluginRegister> plugins     = null,
                                                          Func <IRedisOptionsBuilder, IRedisOptionsBuilder> buildRedisOptions = null)
            where TContext : class, new()
        {
            var options = buildOptions(new RabbitMqOptionsBuilder()).Build();

            return(buildRedisOptions is null
                ? builder.AddRabbitMq <TContext>(options, plugins)
                : builder.AddRabbitMq <TContext>(options, plugins, b => b.AddRedis(buildRedisOptions)));
        }
예제 #27
0
        public static IDrexBuilder AddMongoRepository <TEntity, TIdentifiable>(this IDrexBuilder builder,
                                                                               string collectionName)
            where TEntity : IIdentifiable <TIdentifiable>
        {
            builder.Services.AddTransient <IMongoRepository <TEntity, TIdentifiable> >(sp =>
            {
                var database = sp.GetService <IMongoDatabase>();
                return(new MongoRepository <TEntity, TIdentifiable>(database, collectionName));
            });

            return(builder);
        }
예제 #28
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));
        }
예제 #29
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));
        }
예제 #30
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));
        }