1
1
using Microsoft . Azure . Functions . Extensions . DependencyInjection ;
2
2
using Microsoft . Extensions . Configuration ;
3
- using Microsoft . Extensions . DependencyInjection ;
4
- using Microsoft . Extensions . DependencyInjection . Extensions ;
3
+ using Microsoft . Extensions . Hosting ;
5
4
using System ;
6
5
using System . IO ;
7
- using System . Reflection ;
8
6
9
7
namespace Autofac . Extensions . DependencyInjection . AzureFunctions
10
8
{
@@ -14,57 +12,53 @@ namespace Autofac.Extensions.DependencyInjection.AzureFunctions
14
12
public static class AppSettingsExtensions
15
13
{
16
14
/// <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.
18
16
/// </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 )
22
21
{
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 ( ) ;
25
27
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 ;
41
29
}
42
30
43
31
/// <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.
45
34
/// </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 )
50
38
{
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
+ }
61
41
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
+ }
64
52
65
- hostBuilder . Services . Replace ( ServiceDescriptor . Singleton ( typeof ( IConfiguration ) , configuration ) ) ;
53
+ private static string GetCurrentEnvironmentName ( string contextEnvironmentName )
54
+ {
66
55
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 ;
68
62
}
69
63
}
70
64
}
0 commit comments