|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.Azure.WebJobs.Host.Config; |
| 8 | +using Microsoft.Azure.WebJobs.Host.Scale; |
| 9 | +using Microsoft.Azure.WebJobs.Hosting; |
| 10 | +using Microsoft.Azure.WebJobs.Script.Extensions; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.Extensions.Options; |
| 13 | +using Newtonsoft.Json; |
| 14 | +using Newtonsoft.Json.Linq; |
| 15 | +using Newtonsoft.Json.Serialization; |
| 16 | + |
| 17 | +namespace Microsoft.Azure.WebJobs.Script |
| 18 | +{ |
| 19 | + public class HostOptionsProvider : IHostOptionsProvider |
| 20 | + { |
| 21 | + private readonly JsonSerializer _serializer = JsonSerializer.Create(new JsonSerializerSettings |
| 22 | + { |
| 23 | + ContractResolver = new CamelCasePropertyNamesContractResolver(), |
| 24 | + }); |
| 25 | + |
| 26 | + private readonly IEnumerable<IExtensionOptionsProvider> _extensionOptionsProviders; |
| 27 | + private readonly ILogger<HostOptionsProvider> _logger; |
| 28 | + private readonly IOptionsMonitor<ConcurrencyOptions> _concurrencyOptions; |
| 29 | + |
| 30 | + public HostOptionsProvider(IEnumerable<IExtensionOptionsProvider> extensionOptionsProviders, IOptionsMonitor<ConcurrencyOptions> concurrencyOptions, ILogger<HostOptionsProvider> logger) |
| 31 | + { |
| 32 | + _extensionOptionsProviders = extensionOptionsProviders; |
| 33 | + _logger = logger; |
| 34 | + _concurrencyOptions = concurrencyOptions; |
| 35 | + } |
| 36 | + |
| 37 | + public JObject GetOptions() |
| 38 | + { |
| 39 | + var payload = new JObject(); |
| 40 | + var extensions = new JObject(); |
| 41 | + var extensionOptions = GetExtensionOptions(); |
| 42 | + foreach (var extension in extensionOptions) |
| 43 | + { |
| 44 | + extensions.Add(extension.Key, extension.Value); |
| 45 | + } |
| 46 | + if (extensionOptions.Count != 0) |
| 47 | + { |
| 48 | + payload.Add("extensions", extensions); |
| 49 | + } |
| 50 | + |
| 51 | + var concurrency = GetConcurrencyOptions(); |
| 52 | + payload.Add("concurrency", concurrency); |
| 53 | + return payload; |
| 54 | + } |
| 55 | + |
| 56 | + private Dictionary<string, JObject> GetExtensionOptions() |
| 57 | + { |
| 58 | + Dictionary<string, JObject> result = new Dictionary<string, JObject>(); |
| 59 | + foreach (IExtensionOptionsProvider extensionOptionsProvider in _extensionOptionsProviders) |
| 60 | + { |
| 61 | + var options = extensionOptionsProvider.GetOptions(); |
| 62 | + if (typeof(IOptionsFormatter).IsAssignableFrom(options.GetType())) |
| 63 | + { |
| 64 | + var optionsFormatter = (IOptionsFormatter)options; |
| 65 | + string sectionName = extensionOptionsProvider?.ExtensionInfo?.ConfigurationSectionName?.CamelCaseString(); |
| 66 | + result.Add(sectionName, JObject.Parse(optionsFormatter.Format()).ToCamelCase()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + private JObject GetConcurrencyOptions() |
| 74 | + { |
| 75 | + var concurrencyOptions = _concurrencyOptions.CurrentValue; |
| 76 | + return JObject.FromObject(concurrencyOptions, _serializer); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments