Skip to content

Commit 6173e9b

Browse files
authored
Allow to reuse KM service Builder and config extensions (#901)
Allow to reuse KM configuration utilities in other apps. * `IConfigurationBuilder.AddKernelMemoryConfigurationSources(...)`: adds to .NET ConfigurationBuilder the sources used by KM service, such as `appsettings.json`, `appsettings.<ENV>.json` (not case sensitive), user secrets and env vars. Extension available in [Microsoft.KernelMemory.Core](https://www.nuget.org/packages/Microsoft.KernelMemory.Core) nuget. * `IKernelMemoryBuilder.ConfigureDependencies(...)`: configures and wires up all .NET dependencies accordingly to the given configuration. Extension available in [Microsoft.KernelMemory](https://www.nuget.org/packages/Microsoft.KernelMemory) nuget.
1 parent f119e58 commit 6173e9b

File tree

8 files changed

+189
-203
lines changed

8 files changed

+189
-203
lines changed

service/Service/ServiceConfiguration.cs renamed to extensions/KM/KernelMemory/Internals/KernelMemoryComposer.cs

Lines changed: 124 additions & 140 deletions
Large diffs are not rendered by default.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.KernelMemory.Internals;
5+
6+
// ReSharper disable once CheckNamespace - reduce number of "using" statements
7+
namespace Microsoft.KernelMemory;
8+
9+
/// <summary>
10+
/// Kernel Memory builder extensions for ASP.NET apps using settings in appsettings.json
11+
/// and using IConfiguration. The following methods allow to fully configure KM via
12+
/// IConfiguration, without having to change the code using KernelMemoryBuilder and recompile.
13+
/// </summary>
14+
public static partial class KernelMemoryBuilderExtensions
15+
{
16+
/// <summary>
17+
/// Configure the builder using settings from the given IConfiguration instance.
18+
/// </summary>
19+
/// <param name="builder">KernelMemory builder instance</param>
20+
/// <param name="appSettings">App settings, which might include KM settings</param>
21+
/// <param name="memoryConfig">Optional KM settings, overriding those in appsettings</param>
22+
public static IKernelMemoryBuilder ConfigureDependencies(
23+
this IKernelMemoryBuilder builder,
24+
IConfiguration appSettings,
25+
KernelMemoryConfig? memoryConfig = null)
26+
{
27+
if (appSettings is null)
28+
{
29+
throw new ConfigurationException("The given app settings configuration is NULL");
30+
}
31+
32+
if (memoryConfig is null)
33+
{
34+
memoryConfig = appSettings.GetSection(KernelMemoryComposer.ConfigRoot).Get<KernelMemoryConfig>();
35+
}
36+
37+
if (memoryConfig is null)
38+
{
39+
throw new ConfigurationException($"Unable to load Kernel Memory settings from the given configuration. " +
40+
$"There should be a '{KernelMemoryComposer.ConfigRoot}' root node, " +
41+
$"with data mapping to '{nameof(KernelMemoryConfig)}'");
42+
}
43+
44+
var composer = new KernelMemoryComposer(builder, appSettings, memoryConfig);
45+
composer.ConfigureBuilder();
46+
47+
return builder;
48+
}
49+
}

service/Service/ConfigurationBuilderExtensions.cs renamed to service/Core/Configuration/ConfigurationBuilderExtensions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@
55
using System.Reflection;
66
using Microsoft.Extensions.Configuration;
77

8-
namespace Microsoft.KernelMemory.Service;
8+
#pragma warning disable IDE0130 // reduce number of "using" statements
9+
// ReSharper disable once CheckNamespace - reduce number of "using" statements
10+
namespace Microsoft.KernelMemory;
911

10-
internal static class ConfigurationBuilderExtensions
12+
public static partial class ConfigurationBuilderExtensions
1113
{
1214
// ASP.NET env var
13-
private const string AspnetEnvVar = "ASPNETCORE_ENVIRONMENT";
15+
private const string AspNetCoreEnvVar = "ASPNETCORE_ENVIRONMENT";
1416

15-
public static void AddKMConfigurationSources(
17+
// .NET env var
18+
private const string DotNetEnvVar = "DOTNET_ENVIRONMENT";
19+
20+
public static void AddKernelMemoryConfigurationSources(
1621
this IConfigurationBuilder builder,
1722
bool useAppSettingsFiles = true,
1823
bool useEnvVars = true,
1924
bool useSecretManager = true,
2025
string? settingsDirectory = null)
2126
{
22-
// Load env var name, either Development or Production
23-
var env = Environment.GetEnvironmentVariable(AspnetEnvVar) ?? string.Empty;
27+
// ASPNETCORE_ENVIRONMENT env var takes precedence. Env should be either Development or Production.
28+
var env = Environment.GetEnvironmentVariable(AspNetCoreEnvVar) ?? Environment.GetEnvironmentVariable(DotNetEnvVar) ?? string.Empty;
2429

2530
// Detect the folder containing configuration files
2631
settingsDirectory ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
File renamed without changes.

service/Service/KernelMemoryBuilderExtensions.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

service/Service/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void Main(string[] args)
5757

5858
// *************************** CONFIG WIZARD ***************************
5959

60-
// Run `dotnet run setup` to run this code and setup the service
60+
// Run `dotnet run setup` to run this code and set up the service
6161
if (new[] { "setup", "-setup", "config" }.Contains(args.FirstOrDefault(), StringComparer.OrdinalIgnoreCase))
6262
{
6363
InteractiveSetup.Main.InteractiveSetup(args.Skip(1).ToArray());
@@ -77,7 +77,8 @@ public static void Main(string[] args)
7777
appBuilder.Services.AddApplicationInsightsTelemetry();
7878
}
7979

80-
appBuilder.Configuration.AddKMConfigurationSources();
80+
// Add config files, user secretes, and env vars
81+
appBuilder.Configuration.AddKernelMemoryConfigurationSources();
8182

8283
// Read KM settings, needed before building the app.
8384
KernelMemoryConfig config = appBuilder.Configuration.GetSection("KernelMemory").Get<KernelMemoryConfig>()
@@ -90,7 +91,8 @@ public static void Main(string[] args)
9091
// Internally build the memory client and make it available for dependency injection
9192
appBuilder.AddKernelMemory(memoryBuilder =>
9293
{
93-
memoryBuilder.FromAppSettings().WithoutDefaultHandlers();
94+
// Prepare the builder with settings from config files
95+
memoryBuilder.ConfigureDependencies(appBuilder.Configuration).WithoutDefaultHandlers();
9496

9597
// When using distributed orchestration, handlers are hosted in the current app and need to be con
9698
asyncHandlersCount = AddHandlersAsHostedServices(config, memoryBuilder, appBuilder);

0 commit comments

Comments
 (0)