11using Microsoft . Azure . Functions . Extensions . DependencyInjection ;
22using Microsoft . Extensions . Configuration ;
3- using Microsoft . Extensions . DependencyInjection ;
4- using Microsoft . Extensions . DependencyInjection . Extensions ;
3+ using Microsoft . Extensions . Hosting ;
54using System ;
65using System . IO ;
7- using System . Reflection ;
86
97namespace Autofac . Extensions . DependencyInjection . AzureFunctions
108{
@@ -14,57 +12,53 @@ namespace Autofac.Extensions.DependencyInjection.AzureFunctions
1412 public static class AppSettingsExtensions
1513 {
1614 /// <summary>
17- /// 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.
15+ /// 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 AZURE_FUNCTIONS_ENVIRONMENT environment variable.
1816 /// </summary>
19- /// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param>
20- /// <returns>The IFunctionsHostBuilder.</returns>
21- public static IFunctionsHostBuilder UseAppSettings ( this IFunctionsHostBuilder hostBuilder )
17+ /// <param name="builder">An instance of <see cref="IFunctionsConfigurationBuilder"/>.</param>
18+ /// <param name="reloadOnChange">Whether the configuration should be reloaded on file change.</param>
19+ /// <returns>The <see cref="IFunctionsConfigurationBuilder"/>.</returns>
20+ public static IFunctionsConfigurationBuilder UseAppSettings ( this IFunctionsConfigurationBuilder builder , bool reloadOnChange = false )
2221 {
23- var fileInfo = new FileInfo ( Assembly . GetExecutingAssembly ( ) . Location ) ;
24- string currentDirectory = fileInfo . Directory . Parent . FullName ;
22+ var context = builder . GetContext ( ) ;
23+ builder . ConfigurationBuilder
24+ . AddJsonFile ( Path . Combine ( context . ApplicationRootPath , "appsettings.json" ) , optional : true , reloadOnChange : reloadOnChange )
25+ . AddJsonFile ( Path . Combine ( context . ApplicationRootPath , $ "appsettings.{ builder . GetCurrentEnvironmentName ( ) } .json") , optional : true , reloadOnChange : reloadOnChange )
26+ . AddEnvironmentVariables ( ) ;
2527
26- var environment = Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ;
27- if ( string . IsNullOrWhiteSpace ( environment ) )
28- environment = Environment . GetEnvironmentVariable ( "DOTNET_ENVIRONMENT" ) ;
29- if ( string . IsNullOrWhiteSpace ( environment ) )
30- environment = "Development" ; // Fallback to Development when none is set.
31-
32- return UseAppSettings ( hostBuilder , ( builder ) =>
33- {
34- builder
35- . SetBasePath ( currentDirectory )
36- . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
37- . AddJsonFile ( $ "appsettings.{ environment } .json", optional : true , reloadOnChange : true )
38- . AddJsonFile ( "host.json" , optional : true , reloadOnChange : true )
39- . AddEnvironmentVariables ( ) ;
40- } ) ;
28+ return builder ;
4129 }
4230
4331 /// <summary>
44- /// Creates an <see cref="IConfiguration"/> instance and configures it as declared by <paramref name="configurationAction"/> action.
32+ /// Calculate environment from unreliable function environment setting.
33+ /// github.com/Azure/azure-functions-host/issues/6239 problem determining environment.
4534 /// </summary>
46- /// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param>
47- /// <param name="configurationAction">Action on a <see cref="IConfigurationBuilder"/> that adds configurations to a .NET Core application.</param>
48- /// <returns>The IFunctionsHostBuilder.</returns>
49- public static IFunctionsHostBuilder UseAppSettings ( this IFunctionsHostBuilder hostBuilder , Action < IConfigurationBuilder > configurationAction = null )
35+ /// <param name="builder"></param>
36+ /// <returns></returns>
37+ public static string GetCurrentEnvironmentName ( this IFunctionsHostBuilder builder )
5038 {
51- var configurationBuilder = new ConfigurationBuilder ( ) as IConfigurationBuilder ;
52-
53- using ( var temporaryServiceProvider = hostBuilder . Services . BuildServiceProvider ( ) )
54- {
55- var configRoot = temporaryServiceProvider . GetService < IConfiguration > ( ) ;
56- if ( configRoot != null )
57- {
58- configurationBuilder . AddConfiguration ( configRoot ) ;
59- }
60- }
39+ return GetCurrentEnvironmentName ( builder . GetContext ( ) . EnvironmentName ) ;
40+ }
6141
62- configurationAction ? . Invoke ( configurationBuilder ) ;
63- var configuration = configurationBuilder . Build ( ) ;
42+ /// <summary>
43+ /// Calculate environment from unreliable function environment setting.
44+ /// github.com/Azure/azure-functions-host/issues/6239 problem determining environment.
45+ /// </summary>
46+ /// <param name="builder"></param>
47+ /// <returns></returns>
48+ public static string GetCurrentEnvironmentName ( this IFunctionsConfigurationBuilder builder )
49+ {
50+ return GetCurrentEnvironmentName ( builder . GetContext ( ) . EnvironmentName ) ;
51+ }
6452
65- hostBuilder . Services . Replace ( ServiceDescriptor . Singleton ( typeof ( IConfiguration ) , configuration ) ) ;
53+ private static string GetCurrentEnvironmentName ( string contextEnvironmentName )
54+ {
6655
67- return hostBuilder ;
56+ string environmentName = Environment . GetEnvironmentVariable ( "AZURE_FUNCTIONS_ENVIRONMENT" ) ??
57+ Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ??
58+ Environment . GetEnvironmentVariable ( "DOTNET_ENVIRONMENT" ) ??
59+ contextEnvironmentName ??
60+ EnvironmentName . Development ;
61+ return environmentName ;
6862 }
6963 }
7064}
0 commit comments