diff --git a/src/AppModel/NetDaemon.AppModel/Common/Extensions/IConfigurationBuilderExtensions.cs b/src/AppModel/NetDaemon.AppModel/Common/Extensions/IConfigurationBuilderExtensions.cs
index 1bdf0a209..65c64bd3d 100644
--- a/src/AppModel/NetDaemon.AppModel/Common/Extensions/IConfigurationBuilderExtensions.cs
+++ b/src/AppModel/NetDaemon.AppModel/Common/Extensions/IConfigurationBuilderExtensions.cs
@@ -35,6 +35,30 @@ public static IConfigurationBuilder AddYamlAppConfig(this IConfigurationBuilder
return builder;
}
+ ///
+ /// Adds yaml configurations for apps given the path in the configuration
+ ///
+ /// Builder
+ /// Configuration
+ /// Should be called from `hostBuilder.ConfigureAppConfiguration`
+ ///
+ /// hostBuilder.ConfigureAppConfiguration((context, config) =>
+ /// {
+ /// config.AddYamlAppConfigs(context.Configuration);
+ /// });
+ ///
+ public static IConfigurationBuilder AddYamlAppConfigs(this IConfigurationBuilder builder, IConfiguration configuration)
+ {
+ AppConfigurationLocationSetting? appConfigurationLocationSetting = configuration.GetSection("NetDaemon")?.Get();
+ if (appConfigurationLocationSetting?.ApplicationConfigurationFolder != null)
+ {
+ string fullPath = Path.GetFullPath(appConfigurationLocationSetting.ApplicationConfigurationFolder);
+ builder.AddYamlAppConfig(fullPath);
+ }
+
+ return builder;
+ }
+
internal static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string filePath, bool optional,
bool reloadOnChange)
{
diff --git a/src/Runtime/NetDaemon.Runtime/Common/Extensions/HostBuilderExtensions.cs b/src/Runtime/NetDaemon.Runtime/Common/Extensions/HostBuilderExtensions.cs
index 0895c6bcc..662a0300d 100644
--- a/src/Runtime/NetDaemon.Runtime/Common/Extensions/HostBuilderExtensions.cs
+++ b/src/Runtime/NetDaemon.Runtime/Common/Extensions/HostBuilderExtensions.cs
@@ -7,29 +7,53 @@ namespace NetDaemon.Runtime;
public static class HostBuilderExtensions
{
+ ///
+ /// Call this method to load NetDeamonYaml settings, and to register 'ConfigureNetDaemonServices' in the service collection
+ ///
+ ///
+ ///
+ /// UseNetDaemonAppSettings has several responsibilities:
+ /// - Register appsettings.json to the host configuration
+ /// - Register all the yaml settings from the path set in the current configuration to the configuration provider
+ /// - Call 'ConfigureNetDaemonServices' in the service collection
+ ///
+ /// You can call these methods separately if you want to do something else in between, or if you're calling any of these methods already.
+ /// Change `UseNetDaemonAppSettings` to `.RegisterAppSettingsJsonToHost().RegisterYamlSettings()` and call `ConfigureNetDaemonServices(context.Configuration)` in ConfigureServices.
+ ///
public static IHostBuilder UseNetDaemonAppSettings(this IHostBuilder hostBuilder)
{
return hostBuilder
+ .RegisterAppSettingsJsonToHost()
+ .RegisterYamlSettings()
.ConfigureServices((context, services)
=> services.ConfigureNetDaemonServices(context.Configuration)
- )
- .ConfigureAppConfiguration((ctx, config) =>
- {
- // TODO: Most of this seems to be what Host.CreateDefaultBuilder already does
- config.SetBasePath(Directory.GetCurrentDirectory());
- config.AddJsonFile("appsettings.json");
- config.AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", true);
- config.AddEnvironmentVariables();
-
- var c = config.Build();
- var locationSetting = c.GetSection("NetDaemon").Get();
- if (locationSetting?.ApplicationConfigurationFolder is not null)
- {
- var fullPath = Path.GetFullPath(locationSetting.ApplicationConfigurationFolder);
- config.AddYamlAppConfig(fullPath);
- }
+ );
+ }
- });
+ ///
+ /// Registers appsettings.json to the host configuration
+ ///
+ ///
+ /// This enables using data from the appsettings.json in the `ConfigureAppConfiguration` call
+ public static IHostBuilder RegisterAppSettingsJsonToHost(this IHostBuilder hostBuilder)
+ {
+ return hostBuilder.ConfigureHostConfiguration(config =>
+ {
+ config.AddJsonFile("appsettings.json", optional: true);
+ });
+ }
+
+ ///
+ /// Register all the yaml settings from the path set in the current configuration to the configuration provider
+ ///
+ ///
+ /// Call `RegisterAppSettingsJsonToHost()` before, using this method.
+ public static IHostBuilder RegisterYamlSettings(this IHostBuilder hostBuilder)
+ {
+ return hostBuilder.ConfigureAppConfiguration((context, config) =>
+ {
+ config.AddYamlAppConfigs(context.Configuration);
+ });
}
public static IHostBuilder UseNetDaemonRuntime(this IHostBuilder hostBuilder)