private void AddDatabase(IServiceCollection services, TokenCredential tokenCredential) { // Setup the interceptor that will add access tokens to database connections in Azure var managedIdentityInterceptor = new ManagedIdentityConnectionInterceptor( _environment, tokenCredential); services.AddDbContext <AppDbContext>( o => o.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")).AddInterceptors(managedIdentityInterceptor), ServiceLifetime.Transient); }
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); services.AddSignalR(); services.AddTransient <IDemoService, DemoService>(); services.AddSingleton <IHostedService, QueueListenerService>(); services.AddSingleton <IHostedService, EventHubsListenerService>(); services.Configure <DemoSettings>(Configuration.GetSection("Demo")); DemoSettings demoSettings = Configuration.GetSection("Demo").Get <DemoSettings>(); var managedIdentityInterceptor = new ManagedIdentityConnectionInterceptor(demoSettings); services.AddDbContext <MsiDbContext>(o => o.UseSqlServer(demoSettings.SqlConnectionString).AddInterceptors(managedIdentityInterceptor)); services.AddApplicationInsightsTelemetry(o => { o.EnableQuickPulseMetricStream = true; o.InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"]; }); services.AddHttpClient(HttpClients.CustomApi); }
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSignalR(); services.AddTransient <IDemoService, DemoService>(); services.AddSingleton <IHostedService, QueueListenerService>(); services.AddSingleton <IHostedService, EventHubsListenerService>(); services.Configure <DemoSettings>(Configuration.GetSection("Demo")); DemoSettings demoSettings = Configuration.GetSection("Demo").Get <DemoSettings>(); var managedIdentityInterceptor = new ManagedIdentityConnectionInterceptor(demoSettings); services.AddDbContext <MsiDbContext>(o => o.UseSqlServer(demoSettings.SqlConnectionString).AddInterceptors(managedIdentityInterceptor)); services.AddApplicationInsightsTelemetry(o => { o.EnableQuickPulseMetricStream = true; o.ConnectionString = Configuration["ApplicationInsights:ConnectionString"]; }); var tenantId = string.IsNullOrEmpty(demoSettings.ManagedIdentityTenantId) ? null : demoSettings.ManagedIdentityTenantId; var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { SharedTokenCacheTenantId = tenantId, VisualStudioCodeTenantId = tenantId, VisualStudioTenantId = tenantId, }); services.AddSingleton <TokenCredential>(credential); TokenCredential customApiCredential = string.IsNullOrEmpty(demoSettings.CustomApiClientSecret) ? credential : new ClientSecretCredential( demoSettings.CustomApiTenantId, demoSettings.CustomApiClientId, demoSettings.CustomApiClientSecret); services.AddHttpClient <CustomApiClient, CustomApiClient>( (httpClient, serviceProvider) => { var settings = serviceProvider.GetRequiredService <IOptionsSnapshot <DemoSettings> >(); return(new CustomApiClient(httpClient, settings, customApiCredential)); }); services.AddHttpClient <MapsApiClient>(); services.AddSingleton((IServiceProvider _) => { return(new CosmosClient(Configuration["Demo:CosmosDbAccountUri"], credential)); }); services.AddAzureClients(clients => { clients.AddBlobServiceClient(new Uri($"https://{demoSettings.StorageAccountName}.blob.core.windows.net")); clients.AddDataLakeServiceClient(new Uri($"https://{demoSettings.DataLakeStoreName}.blob.core.windows.net")); clients.AddEventHubProducerClientWithNamespace($"{demoSettings.EventHubNamespace}.servicebus.windows.net", demoSettings.EventHubName); clients.AddServiceBusClientWithNamespace($"{demoSettings.ServiceBusNamespace}.servicebus.windows.net"); clients.AddSecretClient(new Uri(demoSettings.KeyVaultBaseUrl)); clients.AddTextAnalyticsClient(new Uri(demoSettings.CognitiveServicesBaseUrl)); clients.AddConfigurationClient(new Uri(demoSettings.AppConfigUrl)); clients.UseCredential(credential); }); }