|
1 |
| -using Autofac.Builder; |
2 |
| -using Microsoft.Azure.Functions.Extensions.DependencyInjection; |
| 1 | +using Microsoft.Azure.Functions.Extensions.DependencyInjection; |
3 | 2 | using Microsoft.Azure.WebJobs.Host;
|
4 |
| -using Microsoft.Extensions.Configuration; |
5 | 3 | using Microsoft.Extensions.DependencyInjection;
|
6 | 4 | using Microsoft.Extensions.DependencyInjection.Extensions;
|
7 |
| -using Microsoft.Extensions.Logging; |
8 | 5 | using System;
|
9 |
| -using System.IO; |
10 |
| -using System.Linq; |
11 |
| -using System.Reflection; |
12 | 6 |
|
13 | 7 | namespace Autofac.Extensions.DependencyInjection.AzureFunctions
|
14 | 8 | {
|
15 | 9 | /// <summary>
|
16 |
| - /// Extension methods for use with the <see cref="IFunctionsHostBuilder"/> |
| 10 | + /// Extension methods to initialize <see cref="Autofac" /> for use with the <see cref="IFunctionsHostBuilder"/> |
17 | 11 | /// </summary>
|
18 | 12 | public static class ConfigurationExtensions
|
19 | 13 | {
|
@@ -50,97 +44,5 @@ public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunct
|
50 | 44 |
|
51 | 45 | return hostBuilder;
|
52 | 46 | }
|
53 |
| - |
54 |
| - /// <summary> |
55 |
| - /// Creates an <see cref="IConfiguration"/> instance and attaches to an `appsettings.json` file, also adding an `appsettings.{environment}.json` on top of it, if available, based on current ASPNETCORE_ENVIRONMENT environment variable. |
56 |
| - /// </summary> |
57 |
| - /// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param> |
58 |
| - /// <returns>The IFunctionsHostBuilder.</returns> |
59 |
| - public static IFunctionsHostBuilder UseAppSettings(this IFunctionsHostBuilder hostBuilder) |
60 |
| - { |
61 |
| - var fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location); |
62 |
| - string currentDirectory = fileInfo.Directory.Parent.FullName; |
63 |
| - |
64 |
| - var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
65 |
| - if (string.IsNullOrWhiteSpace(environment)) |
66 |
| - environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"); |
67 |
| - if (string.IsNullOrWhiteSpace(environment)) |
68 |
| - environment = "Development"; // Fallback to Development when none is set. |
69 |
| - |
70 |
| - return UseAppSettings(hostBuilder, (builder) => |
71 |
| - { |
72 |
| - builder |
73 |
| - .SetBasePath(currentDirectory) |
74 |
| - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
75 |
| - .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true) |
76 |
| - .AddJsonFile("host.json", optional: true, reloadOnChange: true) |
77 |
| - .AddEnvironmentVariables(); |
78 |
| - }); |
79 |
| - } |
80 |
| - |
81 |
| - /// <summary> |
82 |
| - /// Creates an <see cref="IConfiguration"/> instance and configures it as declared by <paramref name="configurationAction"/> action. |
83 |
| - /// </summary> |
84 |
| - /// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param> |
85 |
| - /// <param name="configurationAction">Action on a <see cref="IConfigurationBuilder"/> that adds configurations to a .NET Core application.</param> |
86 |
| - /// <returns>The IFunctionsHostBuilder.</returns> |
87 |
| - public static IFunctionsHostBuilder UseAppSettings(this IFunctionsHostBuilder hostBuilder, Action<IConfigurationBuilder> configurationAction = null) |
88 |
| - { |
89 |
| - var configurationBuilder = new ConfigurationBuilder() as IConfigurationBuilder; |
90 |
| - |
91 |
| - var descriptor = hostBuilder.Services.FirstOrDefault(d => d.ServiceType == typeof(IConfiguration)); |
92 |
| - if (descriptor?.ImplementationInstance is IConfiguration configRoot) |
93 |
| - { |
94 |
| - configurationBuilder.AddConfiguration(configRoot); |
95 |
| - } |
96 |
| - |
97 |
| - configurationAction?.Invoke(configurationBuilder); |
98 |
| - var configuration = configurationBuilder.Build(); |
99 |
| - |
100 |
| - hostBuilder.Services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), configuration)); |
101 |
| - |
102 |
| - return hostBuilder; |
103 |
| - } |
104 |
| - |
105 |
| - |
106 |
| - /// <summary> |
107 |
| - /// Adds logging services to the specified HostBuilder |
108 |
| - /// </summary> |
109 |
| - /// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param> |
110 |
| - /// <param name="configure">The <see cref="ILoggingBuilder"/> configuration delegate.</param> |
111 |
| - /// <returns>The IFunctionsHostBuilder.</returns> |
112 |
| - public static IFunctionsHostBuilder UseLogger(this IFunctionsHostBuilder hostBuilder, Action<ILoggingBuilder, IConfiguration> configure) |
113 |
| - { |
114 |
| - var configuration = hostBuilder.Services.Where(x => x.ServiceType == typeof(IConfiguration)).SingleOrDefault()?.ImplementationInstance as IConfiguration; |
115 |
| - |
116 |
| - hostBuilder.Services.AddLogging((config) => configure?.Invoke(config, configuration)); |
117 |
| - |
118 |
| - return hostBuilder; |
119 |
| - } |
120 |
| - |
121 |
| - |
122 |
| - |
123 |
| - /// <summary> |
124 |
| - /// Share one instance of the component within the context of a single |
125 |
| - /// azure function trigger request. |
126 |
| - /// </summary> |
127 |
| - /// <typeparam name="TLimit">Registration limit type.</typeparam> |
128 |
| - /// <typeparam name="TActivatorData">Activator data type.</typeparam> |
129 |
| - /// <typeparam name="TStyle">Registration style.</typeparam> |
130 |
| - /// <param name="registration">The registration to configure.</param> |
131 |
| - /// <param name="lifetimeScopeTags">Additional tags applied for matching lifetime scopes.</param> |
132 |
| - /// <returns>A registration builder allowing further configuration of the component.</returns> |
133 |
| - /// <exception cref="System.ArgumentNullException"> |
134 |
| - /// Thrown if <paramref name="registration" /> is <see langword="null" />. |
135 |
| - /// </exception> |
136 |
| - public static IRegistrationBuilder<TLimit, TActivatorData, TStyle> InstancePerTriggerRequest<TLimit, TActivatorData, TStyle>(this IRegistrationBuilder<TLimit, TActivatorData, TStyle> registration, params object[] lifetimeScopeTags) |
137 |
| - { |
138 |
| - if (registration == null) |
139 |
| - throw new ArgumentNullException(nameof(registration)); |
140 |
| - |
141 |
| - var tags = new[] { Scopes.RootLifetimeScopeTag }.Concat(lifetimeScopeTags).ToArray(); |
142 |
| - return registration.InstancePerMatchingLifetimeScope(tags); |
143 |
| - } |
144 |
| - |
145 | 47 | }
|
146 | 48 | }
|
0 commit comments