|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Aspire.Microsoft.Azure.Cosmos; |
| 5 | +using Azure.Identity; |
| 6 | +using Microsoft.Azure.Cosmos; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | + |
| 10 | +namespace Microsoft.Extensions.Hosting; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Azure CosmosDB extension |
| 14 | +/// </summary> |
| 15 | +public static class AspireAzureCosmosDBExtensions |
| 16 | +{ |
| 17 | + private const string DefaultConfigSectionName = "Aspire:Microsoft:Azure:Cosmos"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Registers <see cref="CosmosClient" /> as a singleton in the services provided by the <paramref name="builder"/>. |
| 21 | + /// Configures logging and telemetry for the <see cref="CosmosClient" />. |
| 22 | + /// </summary> |
| 23 | + /// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> |
| 24 | + /// <param name="connectionName">The connection name to use to find a connection string.</param> |
| 25 | + /// <param name="configureSettings">An optional method that can be used for customizing the <see cref="AzureCosmosDBSettings"/>. It's invoked after the settings are read from the configuration.</param> |
| 26 | + /// <param name="configureClientOptions">An optional method that can be used for customizing the <see cref="CosmosClientOptions"/>.</param> |
| 27 | + /// <remarks>Reads the configuration from "Aspire:Microsoft:Azure:Cosmos" section.</remarks> |
| 28 | + /// <exception cref="InvalidOperationException">If required ConnectionString is not provided in configuration section</exception> |
| 29 | + public static void AddAzureCosmosDB( |
| 30 | + this IHostApplicationBuilder builder, |
| 31 | + string connectionName, |
| 32 | + Action<AzureCosmosDBSettings>? configureSettings = null, |
| 33 | + Action<CosmosClientOptions>? configureClientOptions = null) |
| 34 | + { |
| 35 | + AddAzureCosmosDB(builder, DefaultConfigSectionName, configureSettings, configureClientOptions, connectionName, serviceKey: null); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Registers <see cref="CosmosClient" /> as a singleton for given <paramref name="name" /> in the services provided by the <paramref name="builder"/>. |
| 40 | + /// Configures logging and telemetry for the <see cref="CosmosClient" />. |
| 41 | + /// </summary> |
| 42 | + /// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param> |
| 43 | + /// <param name="name">The name of the component, which is used as the <see cref="ServiceDescriptor.ServiceKey"/> of the service and also to retrieve the connection string from the ConnectionStrings configuration section.</param> |
| 44 | + /// <param name="configureSettings">An optional method that can be used for customizing the <see cref="AzureCosmosDBSettings"/>. It's invoked after the settings are read from the configuration.</param> |
| 45 | + /// <param name="configureClientOptions">An optional method that can be used for customizing the <see cref="CosmosClientOptions"/>.</param> |
| 46 | + /// <remarks>Reads the configuration from "Aspire:Microsoft:Azure:Cosmos:{name}" section.</remarks> |
| 47 | + /// <exception cref="InvalidOperationException">If required ConnectionString is not provided in configuration section</exception> |
| 48 | + public static void AddKeyedAzureCosmosDB( |
| 49 | + this IHostApplicationBuilder builder, |
| 50 | + string name, |
| 51 | + Action<AzureCosmosDBSettings>? configureSettings = null, |
| 52 | + Action<CosmosClientOptions>? configureClientOptions = null) |
| 53 | + { |
| 54 | + AddAzureCosmosDB(builder, $"{DefaultConfigSectionName}:{name}", configureSettings, configureClientOptions, connectionName: name, serviceKey: name); |
| 55 | + } |
| 56 | + |
| 57 | + private static void AddAzureCosmosDB( |
| 58 | + this IHostApplicationBuilder builder, |
| 59 | + string configurationSectionName, |
| 60 | + Action<AzureCosmosDBSettings>? configureSettings, |
| 61 | + Action<CosmosClientOptions>? configureClientOptions, |
| 62 | + string connectionName, |
| 63 | + string? serviceKey) |
| 64 | + { |
| 65 | + ArgumentNullException.ThrowIfNull(builder); |
| 66 | + |
| 67 | + var settings = new AzureCosmosDBSettings(); |
| 68 | + builder.Configuration.GetSection(configurationSectionName).Bind(settings); |
| 69 | + |
| 70 | + if (builder.Configuration.GetConnectionString(connectionName) is string connectionString) |
| 71 | + { |
| 72 | + if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri)) |
| 73 | + { |
| 74 | + settings.AccountEndpoint = uri; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + settings.ConnectionString = connectionString; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + configureSettings?.Invoke(settings); |
| 83 | + |
| 84 | + var clientOptions = new CosmosClientOptions(); |
| 85 | + // Needs to be enabled for either logging or tracing to work. |
| 86 | + clientOptions.CosmosClientTelemetryOptions.DisableDistributedTracing = false; |
| 87 | + if (settings.Tracing) |
| 88 | + { |
| 89 | + builder.Services.AddOpenTelemetry().WithTracing(tracerProviderBuilder => |
| 90 | + { |
| 91 | + tracerProviderBuilder.AddSource("Azure.Cosmos.Operation"); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + configureClientOptions?.Invoke(clientOptions); |
| 96 | + |
| 97 | + if (serviceKey is null) |
| 98 | + { |
| 99 | + builder.Services.AddSingleton(_ => ConfigureDb()); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + builder.Services.AddKeyedSingleton(serviceKey, (sp, key) => ConfigureDb()); |
| 104 | + } |
| 105 | + |
| 106 | + CosmosClient ConfigureDb() |
| 107 | + { |
| 108 | + if (!string.IsNullOrEmpty(settings.ConnectionString)) |
| 109 | + { |
| 110 | + return new CosmosClient(settings.ConnectionString, clientOptions); |
| 111 | + } |
| 112 | + else if (settings.AccountEndpoint is not null) |
| 113 | + { |
| 114 | + var credential = settings.Credential ?? new DefaultAzureCredential(); |
| 115 | + return new CosmosClient(settings.AccountEndpoint.OriginalString, credential, clientOptions); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + throw new InvalidOperationException( |
| 120 | + $"A CosmosClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or either " + |
| 121 | + $"{nameof(settings.ConnectionString)} or {nameof(settings.AccountEndpoint)} must be provided " + |
| 122 | + $"in the '{configurationSectionName}' configuration section."); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments