| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Reflection; |
| | 7 | | using Microsoft.Extensions.Options; |
| | 8 | |
|
| | 9 | | namespace Microsoft.Extensions.Azure |
| | 10 | | { |
| | 11 | | // Slightly adjusted copy of https://github.com/aspnet/Extensions/blob/master/src/Options/Options/src/OptionsFactory |
| | 12 | | internal class ClientOptionsFactory<TClient, TOptions> where TOptions : class |
| | 13 | | { |
| | 14 | | private const string ServiceVersionParameterTypeName = "ServiceVersion"; |
| | 15 | |
|
| | 16 | | private readonly IEnumerable<IConfigureOptions<TOptions>> _setups; |
| | 17 | | private readonly IEnumerable<IPostConfigureOptions<TOptions>> _postConfigures; |
| | 18 | |
|
| | 19 | | private readonly IEnumerable<ClientRegistration<TClient, TOptions>> _clientRegistrations; |
| | 20 | |
|
| 42 | 21 | | public ClientOptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IPostConfigureOptions<T |
| | 22 | | { |
| 42 | 23 | | _setups = setups; |
| 42 | 24 | | _postConfigures = postConfigures; |
| 42 | 25 | | _clientRegistrations = clientRegistrations; |
| 42 | 26 | | } |
| | 27 | |
|
| | 28 | | private TOptions CreateOptions(string name) |
| | 29 | | { |
| 42 | 30 | | object version = null; |
| | 31 | |
|
| 180 | 32 | | foreach (var clientRegistration in _clientRegistrations) |
| | 33 | | { |
| 48 | 34 | | if (clientRegistration.Name == name) |
| | 35 | | { |
| 44 | 36 | | version = clientRegistration.Version; |
| | 37 | | } |
| | 38 | | } |
| | 39 | |
|
| 42 | 40 | | ConstructorInfo parameterlessConstructor = null; |
| 42 | 41 | | int versionParameterIndex = 0; |
| 42 | 42 | | object[] constructorArguments = null; |
| | 43 | |
|
| 168 | 44 | | foreach (var constructor in typeof(TOptions).GetConstructors()) |
| | 45 | | { |
| 42 | 46 | | var parameters = constructor.GetParameters(); |
| 42 | 47 | | if (parameters.Length == 0) |
| | 48 | | { |
| 0 | 49 | | parameterlessConstructor = constructor; |
| 0 | 50 | | continue; |
| | 51 | | } |
| | 52 | |
|
| 42 | 53 | | bool allParametersHaveDefaultValue = true; |
| 176 | 54 | | for (int i = 0; i < parameters.Length; i++) |
| | 55 | | { |
| 46 | 56 | | ParameterInfo parameter = parameters[i]; |
| 46 | 57 | | if (parameter.HasDefaultValue) |
| | 58 | | { |
| 46 | 59 | | if (IsServiceVersionParameter(parameter)) |
| | 60 | | { |
| 42 | 61 | | versionParameterIndex = i; |
| | 62 | | } |
| | 63 | | } |
| | 64 | | else |
| | 65 | | { |
| 0 | 66 | | allParametersHaveDefaultValue = false; |
| 0 | 67 | | break; |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| 42 | 71 | | if (allParametersHaveDefaultValue) |
| | 72 | | { |
| 42 | 73 | | constructorArguments = new object[parameters.Length]; |
| | 74 | |
|
| 176 | 75 | | for (int i = 0; i < parameters.Length; i++) |
| | 76 | | { |
| 46 | 77 | | constructorArguments[i] = parameters[i].DefaultValue; |
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| 42 | 82 | | if (version != null) |
| | 83 | | { |
| 4 | 84 | | if (constructorArguments != null) |
| | 85 | | { |
| 4 | 86 | | constructorArguments[versionParameterIndex] = version; |
| 4 | 87 | | return (TOptions)Activator.CreateInstance(typeof(TOptions), constructorArguments); |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | throw new InvalidOperationException("Unable to find constructor that takes service version"); |
| | 91 | | } |
| | 92 | |
|
| 38 | 93 | | if (parameterlessConstructor != null) |
| | 94 | | { |
| 0 | 95 | | return Activator.CreateInstance<TOptions>(); |
| | 96 | | } |
| | 97 | |
|
| 38 | 98 | | return (TOptions)Activator.CreateInstance(typeof(TOptions), constructorArguments); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | private static bool IsServiceVersionParameter(ParameterInfo parameter) => |
| 46 | 102 | | parameter.ParameterType.Name == ServiceVersionParameterTypeName; |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Returns a configured <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>. |
| | 106 | | /// </summary> |
| | 107 | | public TOptions Create(string name) |
| | 108 | | { |
| 42 | 109 | | var options = CreateOptions(name); |
| 204 | 110 | | foreach (var setup in _setups) |
| | 111 | | { |
| 60 | 112 | | if (setup is IConfigureNamedOptions<TOptions> namedSetup) |
| | 113 | | { |
| 60 | 114 | | namedSetup.Configure(name, options); |
| | 115 | | } |
| 0 | 116 | | else if (name == Microsoft.Extensions.Options.Options.DefaultName) |
| | 117 | | { |
| 0 | 118 | | setup.Configure(options); |
| | 119 | | } |
| | 120 | | } |
| 0 | 121 | | foreach (var post in _postConfigures) |
| | 122 | | { |
| 0 | 123 | | post.PostConfigure(name, options); |
| | 124 | | } |
| | 125 | |
|
| 42 | 126 | | return options; |
| | 127 | | } |
| | 128 | | } |
| | 129 | | } |