|
| 1 | +namespace todo |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Threading.Tasks; |
| 5 | + using Azure.Identity; |
| 6 | + using Microsoft.AspNetCore.Builder; |
| 7 | + using Microsoft.AspNetCore.Hosting; |
| 8 | + using Microsoft.Extensions.Configuration; |
| 9 | + using Microsoft.Extensions.DependencyInjection; |
| 10 | + using Microsoft.Extensions.Hosting; |
| 11 | + |
| 12 | + public class Startup |
| 13 | + { |
| 14 | + public Startup(IConfiguration configuration) |
| 15 | + { |
| 16 | + Configuration = configuration; |
| 17 | + } |
| 18 | + |
| 19 | + public IConfiguration Configuration { get; } |
| 20 | + |
| 21 | + // <ConfigureServices> |
| 22 | + public void ConfigureServices(IServiceCollection services) |
| 23 | + { |
| 24 | + services.AddControllersWithViews(); |
| 25 | + services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult()); |
| 26 | + } |
| 27 | + // </ConfigureServices> |
| 28 | + |
| 29 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 30 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 31 | + { |
| 32 | + if (env.IsDevelopment()) |
| 33 | + { |
| 34 | + app.UseDeveloperExceptionPage(); |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + app.UseExceptionHandler("/Home/Error"); |
| 39 | + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
| 40 | + app.UseHsts(); |
| 41 | + } |
| 42 | + app.UseHttpsRedirection(); |
| 43 | + app.UseStaticFiles(); |
| 44 | + |
| 45 | + app.UseRouting(); |
| 46 | + |
| 47 | + app.UseAuthorization(); |
| 48 | + |
| 49 | + app.UseEndpoints(endpoints => |
| 50 | + { |
| 51 | + endpoints.MapControllerRoute( |
| 52 | + name: "default", |
| 53 | + pattern: "{controller=Item}/{action=Index}/{id?}"); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + // <InitializeCosmosClientInstanceAsync> |
| 58 | + /// <summary> |
| 59 | + /// Creates a Cosmos DB database and a container with the specified partition key. |
| 60 | + /// </summary> |
| 61 | + /// <returns></returns> |
| 62 | + private static async Task<CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection) |
| 63 | + { |
| 64 | + string databaseName = configurationSection.GetSection("DatabaseName").Value; |
| 65 | + string containerName = configurationSection.GetSection("ContainerName").Value; |
| 66 | + string account = configurationSection.GetSection("Account").Value; |
| 67 | + |
| 68 | + // If key is not set, assume we're using managed identity |
| 69 | + string key = configurationSection.GetSection("Key").Value; |
| 70 | + ManagedIdentityCredential miCredential; |
| 71 | + Microsoft.Azure.Cosmos.CosmosClient client; |
| 72 | + if (string.IsNullOrEmpty(key)) |
| 73 | + { |
| 74 | + miCredential = new ManagedIdentityCredential(); |
| 75 | + client = new Microsoft.Azure.Cosmos.CosmosClient(account, miCredential); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + client = new Microsoft.Azure.Cosmos.CosmosClient(account, key); |
| 80 | + } |
| 81 | + |
| 82 | + CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName); |
| 83 | + Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName); |
| 84 | + await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id"); |
| 85 | + |
| 86 | + return cosmosDbService; |
| 87 | + } |
| 88 | + // </InitializeCosmosClientInstanceAsync> |
| 89 | + } |
| 90 | +} |
0 commit comments