-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AzureClientBuilderExtensions.cs
128 lines (119 loc) · 7.5 KB
/
AzureClientBuilderExtensions.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Azure.Core;
using Azure.Core.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.Azure
{
/// <summary>
/// Extension methods to configure client registrations.
/// </summary>
public static class AzureClientBuilderExtensions
{
/// <summary>
/// Sets the name for the client registration. To resolve named clients use <see cref="IAzureClientFactory{TClient}.CreateClient"/> method.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="name">The name to set.</param>
/// <returns>The client builder instance.</returns>
public static IAzureClientBuilder<TClient, TOptions> WithName<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder, string name) where TOptions : class
{
builder.ToBuilder().Registration.Name = name;
return builder;
}
/// <summary>
/// Set the credential to use for this client registration.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="credential">The credential to use.</param>
/// <returns>The client builder instance.</returns>
public static IAzureClientBuilder<TClient, TOptions> WithCredential<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder, TokenCredential credential) where TOptions : class
{
return builder.WithCredential(_ => credential);
}
/// <summary>
/// Set the credential factory to use for this client registration.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="credentialFactory">The credential factory to use.</param>
/// <returns>The client builder instance.</returns>
public static IAzureClientBuilder<TClient, TOptions> WithCredential<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder, Func<IServiceProvider, TokenCredential> credentialFactory) where TOptions : class
{
var impl = builder.ToBuilder();
impl.ServiceCollection.AddSingleton<IConfigureOptions<AzureClientCredentialOptions<TClient>>>(new ConfigureClientCredentials<TClient,TOptions>(impl.Registration, credentialFactory));
return builder;
}
/// <summary>
/// Adds a delegate to configure the client options. All delegates are executed in order they were added.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="configureOptions">The delegate to use to configure options.</param>
/// <returns>The client builder instance.</returns>
public static IAzureClientBuilder<TClient, TOptions> ConfigureOptions<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder, Action<TOptions> configureOptions) where TOptions : class
{
return builder.ConfigureOptions((options, _) => configureOptions(options));
}
/// <summary>
/// Configures client options using provided <see cref="IConfiguration"/> instance.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="configuration">The configuration instance to use.</param>
/// <returns>The client builder instance.</returns>
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
public static IAzureClientBuilder<TClient, TOptions> ConfigureOptions<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder, IConfiguration configuration) where TOptions : class
{
return builder.ConfigureOptions(options => configuration.Bind(options));
}
/// <summary>
/// Adds a delegate to configure the client options. All delegates are executed in order they were added.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="configureOptions">The delegate to use to configure options.</param>
/// <returns>The client builder instance.</returns>
public static IAzureClientBuilder<TClient, TOptions> ConfigureOptions<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder, Action<TOptions, IServiceProvider> configureOptions) where TOptions : class
{
var impl = builder.ToBuilder();
impl.ServiceCollection.AddSingleton<IConfigureOptions<TOptions>>(provider => new ConfigureClientOptions<TClient,TOptions>(provider, impl.Registration, configureOptions));;
return builder;
}
/// <summary>
/// Sets the service version to use for this client registration.
/// </summary>
/// <typeparam name="TClient">The type of the client.</typeparam>
/// <typeparam name="TOptions">The options type the client uses.</typeparam>
/// <typeparam name="TVersion">The service version enum type.</typeparam>
/// <param name="builder">The client builder instance.</param>
/// <param name="version">The delegate to use to configure options.</param>
/// <returns>The client builder instance.</returns>
public static IAzureClientBuilder<TClient, TOptions> WithVersion<TClient, TOptions, TVersion>(this IAzureClientBuilder<TClient, TOptions> builder, TVersion version) where TOptions : class
{
if (typeof(TVersion).DeclaringType != typeof(TOptions))
{
throw new ArgumentException($"Version should be of type {typeof(TOptions)}.ServiceVersion");
}
builder.ToBuilder().Registration.Version = version;
return builder;
}
private static AzureClientBuilder<TClient, TOptions> ToBuilder<TClient, TOptions>(this IAzureClientBuilder<TClient, TOptions> builder) where TOptions : class
{
return (AzureClientBuilder<TClient, TOptions>)builder;
}
}
}