|
| 1 | +using System; |
| 2 | +using Azure.Data.Tables; |
| 3 | +using Microsoft.Extensions.Azure; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | + |
| 7 | +namespace Microsoft.Azure.Functions.Worker.Extensions.Tables.Config |
| 8 | +{ |
| 9 | + internal class TablesBindingOptionsSetup : IConfigureNamedOptions<TablesBindingOptions> |
| 10 | + { |
| 11 | + private readonly IConfiguration _configuration; |
| 12 | + private readonly AzureComponentFactory _componentFactory; |
| 13 | + |
| 14 | + private const string TablesServiceUriSubDomain = "table"; |
| 15 | + |
| 16 | + public TablesBindingOptionsSetup(IConfiguration configuration, AzureComponentFactory componentFactory) |
| 17 | + { |
| 18 | + _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); |
| 19 | + _componentFactory = componentFactory ?? throw new ArgumentNullException(nameof(componentFactory)); |
| 20 | + } |
| 21 | + |
| 22 | + public void Configure(TablesBindingOptions options) |
| 23 | + { |
| 24 | + Configure(Options.DefaultName, options); |
| 25 | + } |
| 26 | + |
| 27 | + public void Configure(string name, TablesBindingOptions options) |
| 28 | + { |
| 29 | + if (string.IsNullOrWhiteSpace(name)) |
| 30 | + { |
| 31 | + name = Constants.Storage; // default |
| 32 | + } |
| 33 | + |
| 34 | + IConfigurationSection connectionSection = _configuration.GetWebJobsConnectionStringSection(name); |
| 35 | + |
| 36 | + if (!connectionSection.Exists()) |
| 37 | + { |
| 38 | + // Not found |
| 39 | + throw new InvalidOperationException($"Tables connection configuration '{name}' does not exist. " + |
| 40 | + "Make sure that it is a defined App Setting."); |
| 41 | + } |
| 42 | + |
| 43 | + if (!string.IsNullOrWhiteSpace(connectionSection.Value)) |
| 44 | + { |
| 45 | + options.ConnectionString = connectionSection.Value; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + if (connectionSection.TryGetServiceUriForStorageAccounts(TablesServiceUriSubDomain, out Uri serviceUri)) |
| 50 | + { |
| 51 | + options.ServiceUri = serviceUri; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + options.TableClientOptions = (TableClientOptions)_componentFactory.CreateClientOptions(typeof(TableClientOptions), null, connectionSection); |
| 56 | + options.Credential = _componentFactory.CreateTokenCredential(connectionSection); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments