public static IKitbagBuilder AddWebApi(this IKitbagBuilder builder, string sectionName = "WebApi") { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(); var webApiOptions = builder.GetSettings <WebApiOptions>(sectionName); var corsAllowedOrigins = webApiOptions.CorsAllowedOrigins?.ToArray() ?? new string[0]; builder.Services .AddMvcCore(option => option.EnableEndpointRouting = false) .AddNewtonsoftJson(options => { options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset; options.SerializerSettings.Converters.Add(new StringEnumConverter()); }) .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }) .AddApplicationPart(assembly) .AddControllersAsServices() .AddAuthorization() .AddApiExplorer() .AddCors(options => options.AddPolicy( AllowedSpecificOrigins, b => b.WithOrigins(corsAllowedOrigins) .AllowAnyHeader() .AllowAnyMethod() )); return(builder); }
public static IKitbagBuilder AddEntityFramework <TDbContext>(this IKitbagBuilder builder, string sectionName = "Database", string?subsectionName = null) where TDbContext : DbContext { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var section = string.IsNullOrEmpty(subsectionName) ? sectionName : $"{sectionName}:{subsectionName}"; var properties = builder.GetSettings <PersistenceProperties>(section); if (properties.ConnectionString == null) { throw new ArgumentNullException("ConnectionString is empty"); } builder.Services.AddSingleton(properties); builder.Services.AddDbContext <TDbContext>(optionsBuilder => { optionsBuilder.UseSqlServer(properties.ConnectionString); optionsBuilder.ReplaceService <IValueConverterSelector, StronglyTypedIdValueConverterSelector>(); }); builder.Services.AddScoped <DbContext>(p => p.GetRequiredService <TDbContext>()); return(builder); }
public static IKitbagBuilder AddAppInsights(this IKitbagBuilder builder, string sectionName = "Logging") { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var loggingProperties = builder.GetSettings <LoggingProperties>(sectionName); builder.Services.AddSingleton(loggingProperties); if (!Enum.TryParse <LogLevel>(loggingProperties?.LogLevel?.Default, true, out var level)) { level = LogLevel.Information; } builder.Services.AddLogging(loggerBuilder => { string?key = loggingProperties?.ApplicationInsights?.InstrumentationKey; if (key == null) { throw new ArgumentException("Instrumentation Key is missing"); } loggerBuilder.AddApplicationInsights(key); loggerBuilder.AddFilter <ApplicationInsightsLoggerProvider>("", level); }); builder.Services.AddApplicationInsightsTelemetry(); return(builder); }
public static IKitbagBuilder AddSwagger(this IKitbagBuilder builder, string sectionName = "Swagger") { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var swaggerProperties = builder.GetSettings <SwaggerProperties>(sectionName); builder.Services.AddSingleton(swaggerProperties); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc(swaggerProperties.ApiVersion, new OpenApiInfo { Title = swaggerProperties.ApiName, Version = swaggerProperties.ApiVersion, Description = swaggerProperties.ApiDescription }); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var xmlPaths = assemblies .Select(a => Path.Combine(AppContext.BaseDirectory, $"{a.GetName().Name}.xml")) .Where(a => File.Exists(a)) .ToList(); xmlPaths.ForEach(x => c.IncludeXmlComments(x)); }); return(builder); }
public static IKitbagBuilder AddAzureAD( this IKitbagBuilder builder, string sectionName = "AzureAD", string?subsectionName = null) { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var section = string.IsNullOrEmpty(subsectionName) ? sectionName : $"{sectionName}:{subsectionName}"; builder.Services.AddProtectedWebApi(options => { builder.GetSettings(section, options); options.Events = new JwtBearerEvents(); options.Events.OnAuthenticationFailed = (c) => { if (c.Exception is UnauthorizedAccessException) { c.Fail(c.Exception); } return(Task.CompletedTask); }; options.Events.OnChallenge = (c) => { if (string.IsNullOrEmpty(c.ErrorDescription)) { c.ErrorDescription = c.AuthenticateFailure?.Message; } return(Task.CompletedTask); }; }, options => { builder.GetSettings(section, options); }); return(builder); }
public static IKitbagBuilder AddHttpClient(this IKitbagBuilder builder, string sectionName = "httpClient") { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var properties = builder.GetSettings <HttpClientProperties>(sectionName); builder.Services.AddSingleton(properties); builder.Services.AddHttpClient <IHttpClient, KitBagHttpClient>("kitbag.httpclient"); builder.Services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); return(builder); }
public static IKitbagBuilder AddAzureQueue(this IKitbagBuilder builder, string sectionName = "MessageBus") { if (!builder.TryRegisterKitBag(sectionName)) { return(builder); } var busProperties = builder.GetSettings <BusProperties>(sectionName); builder.Services.AddSingleton(busProperties); builder.Services.AddSingleton <IAzureQueueClient, AzureQueueClient>(); builder.Services.AddSingleton <IEventBusSubscriber, QueueEventSubscriber>(); builder.Services.AddSingleton <IBusPublisher, QueuePublisher>(); return(builder); }