-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
8 changed files
with
189 additions
and
203 deletions.
There are no files selected for viewing
264 changes: 124 additions & 140 deletions
264
service/Service/ServiceConfiguration.cs → ...lMemory/Internals/KernelMemoryComposer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
extensions/KM/KernelMemory/KernelMemoryBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.KernelMemory.Internals; | ||
|
||
// ReSharper disable once CheckNamespace - reduce number of "using" statements | ||
namespace Microsoft.KernelMemory; | ||
|
||
/// <summary> | ||
/// Kernel Memory builder extensions for ASP.NET apps using settings in appsettings.json | ||
/// and using IConfiguration. The following methods allow to fully configure KM via | ||
/// IConfiguration, without having to change the code using KernelMemoryBuilder and recompile. | ||
/// </summary> | ||
public static partial class KernelMemoryBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configure the builder using settings from the given IConfiguration instance. | ||
/// </summary> | ||
/// <param name="builder">KernelMemory builder instance</param> | ||
/// <param name="appSettings">App settings, which might include KM settings</param> | ||
/// <param name="memoryConfig">Optional KM settings, overriding those in appsettings</param> | ||
public static IKernelMemoryBuilder ConfigureDependencies( | ||
this IKernelMemoryBuilder builder, | ||
IConfiguration appSettings, | ||
KernelMemoryConfig? memoryConfig = null) | ||
{ | ||
if (appSettings is null) | ||
{ | ||
throw new ConfigurationException("The given app settings configuration is NULL"); | ||
} | ||
|
||
if (memoryConfig is null) | ||
{ | ||
memoryConfig = appSettings.GetSection(KernelMemoryComposer.ConfigRoot).Get<KernelMemoryConfig>(); | ||
} | ||
|
||
if (memoryConfig is null) | ||
{ | ||
throw new ConfigurationException($"Unable to load Kernel Memory settings from the given configuration. " + | ||
$"There should be a '{KernelMemoryComposer.ConfigRoot}' root node, " + | ||
$"with data mapping to '{nameof(KernelMemoryConfig)}'"); | ||
} | ||
|
||
var composer = new KernelMemoryComposer(builder, appSettings, memoryConfig); | ||
composer.ConfigureBuilder(); | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters