private static void BuildDataContext(IFunctionsHostBuilder builder) { builder.Services.AddDbContext <ApplyDataContext>((serviceProvider, options) => { var connectionStrings = serviceProvider.GetService <IOptions <ConnectionStrings> >().Value; var applySqlConnectionString = connectionStrings.ApplySqlConnectionString; var connection = new SqlConnection(applySqlConnectionString); var configuration = serviceProvider.GetService <IConfiguration>(); if (!configuration["EnvironmentName"].Equals("LOCAL", StringComparison.CurrentCultureIgnoreCase)) { var generateTokenTask = SqlTokenGenerator.GenerateTokenAsync(); connection.AccessToken = generateTokenTask.GetAwaiter().GetResult(); } options.UseSqlServer(connection); }); }
public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.Configure <QnAConfig>(Configuration.GetSection("QnA")); services.Configure <AzureActiveDirectoryConfiguration>(Configuration.GetSection("AzureActiveDirectoryConfiguration")); services.Configure <FileStorageConfig>(Configuration.GetSection("FileStorage")); var serviceProvider = services.BuildServiceProvider(); var config = serviceProvider.GetService <IOptions <QnAConfig> >(); IdentityModelEventSource.ShowPII = true; services.AddApiAuthorization(_hostingEnvironment); services.AddApiAuthentication(serviceProvider); services.RegisterAllTypes <IValidator>(new[] { typeof(IValidator).Assembly }); services.AddTransient <IValidatorFactory, ValidatorFactory>(); services.AddTransient <IAnswerValidator, AnswerValidator>(); services.AddTransient <IFileContentValidator, FileContentValidator>(); services.AddTransient <IApplicationDataValidator, ApplicationDataValidator>(); services.AddTransient <IEncryptionService, EncryptionService>(); services.AddTransient <INotRequiredProcessor, NotRequiredProcessor>(); services.AddTransient <IKeyProvider, ConfigKeyProvider>(); services.AddTransient <ITagProcessingService, TagProcessingService>(); services.AddAutoMapper(typeof(SystemTime).Assembly); services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies()); services.AddDbContext <QnaDataContext>(options => { var qnaSqlConnectionString = config.Value.SqlConnectionstring; var connection = new System.Data.SqlClient.SqlConnection(qnaSqlConnectionString); if (!_hostingEnvironment.IsDevelopment()) { var generateTokenTask = SqlTokenGenerator.GenerateTokenAsync(); connection.AccessToken = generateTokenTask.GetAwaiter().GetResult(); } options.UseSqlServer(connection, providerOptions => providerOptions.EnableRetryOnFailure()); }); services.AddEntityFrameworkSqlServer(); services.AddMvc(setup => { if (!_hostingEnvironment.IsDevelopment()) { setup.Filters.Add(new AuthorizeFilter("default")); } setup.Conventions.Add(new ApiExplorerGroupConvention()); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "QnA API", Version = "0.1" }); c.SwaggerDoc("config", new Info { Title = "QnA API Config", Version = "0.1" }); if (_hostingEnvironment.IsDevelopment()) { var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); } }); services.AddHealthChecks().AddDbContextCheck <QnaDataContext>(); }