-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathAzureAppConfigurationSource.cs
More file actions
93 lines (79 loc) · 3.99 KB
/
AzureAppConfigurationSource.cs
File metadata and controls
93 lines (79 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Azure.Core;
using Azure.Data.AppConfiguration;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.Cdn;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
{
internal class AzureAppConfigurationSource : IConfigurationSource
{
private readonly bool _optional;
private readonly Func<AzureAppConfigurationOptions> _optionsProvider;
public AzureAppConfigurationSource(Action<AzureAppConfigurationOptions> optionsInitializer, bool optional = false)
{
_optionsProvider = () =>
{
var options = new AzureAppConfigurationOptions();
optionsInitializer(options);
return options;
};
_optional = optional;
}
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
IConfigurationProvider provider = null;
try
{
AzureAppConfigurationOptions options = _optionsProvider();
if (options.ClientManager != null)
{
return new AzureAppConfigurationProvider(options.ClientManager, options, _optional);
}
IEnumerable<Uri> endpoints;
IAzureClientFactory<ConfigurationClient> clientFactory = options.ClientFactory;
if (options.ConnectionStrings != null)
{
endpoints = options.ConnectionStrings.Select(cs => new Uri(ConnectionStringUtils.Parse(cs, ConnectionStringUtils.EndpointSection)));
clientFactory ??= new AzureAppConfigurationClientFactory(options.ConnectionStrings, options.ClientOptions);
}
else if (options.Endpoints != null && options.Credential != null)
{
endpoints = options.Endpoints;
clientFactory ??= new AzureAppConfigurationClientFactory(options.Credential, options.ClientOptions);
}
else
{
throw new ArgumentException($"Please call {nameof(AzureAppConfigurationOptions)}.{nameof(AzureAppConfigurationOptions.Connect)} or {nameof(AzureAppConfigurationOptions)}.{nameof(AzureAppConfigurationOptions.ConnectCdn)} to specify how to connect to Azure App Configuration.");
}
if (options.IsCdnEnabled)
{
if (options.LoadBalancingEnabled)
{
throw new InvalidOperationException("Load balancing is not supported for CDN endpoint.");
}
options.CdnTokenAccessor = new CdnTokenAccessor();
options.ClientOptions.AddPolicy(new CdnPolicy(options.CdnTokenAccessor), HttpPipelinePosition.PerCall);
provider = new AzureAppConfigurationProvider(new CdnConfigurationClientManager(clientFactory, endpoints.First()), options, _optional);
}
else
{
provider = new AzureAppConfigurationProvider(new ConfigurationClientManager(clientFactory, endpoints, options.ReplicaDiscoveryEnabled, options.LoadBalancingEnabled), options, _optional);
}
}
catch (InvalidOperationException ex) // InvalidOperationException is thrown when any problems are found while configuring AzureAppConfigurationOptions or when SDK fails to create a configurationClient.
{
throw new ArgumentException(ex.Message, ex);
}
catch (FormatException fe) // FormatException is thrown when the connection string is not a well formed connection string.
{
throw new ArgumentException(fe.Message, fe);
}
return provider ?? new EmptyConfigurationProvider();
}
}
}