Files
TheXamlGuy/Framework/Core/Extensions/IHostBuilderExtensions.cs
2022-11-01 15:26:08 +00:00

37 lines
1.3 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace TheXamlGuy.Framework.Core
{
public static class IHostBuilderExtensions
{
public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder, string contentRoot, bool createDirectory)
{
if (!Directory.Exists(contentRoot) && createDirectory)
{
Directory.CreateDirectory(contentRoot);
}
return hostBuilder.UseContentRoot(contentRoot);
}
public static IHostBuilder ConfigureEvents(this IHostBuilder hostBuilder, Action<IEventBuilder> builderDelegate)
{
hostBuilder.ConfigureServices((hostBuilderContext, serviceCollection) =>
{
EventBuilder? builder = new();
builderDelegate?.Invoke(builder);
foreach (IEventBuilderConfiguration? configuration in builder.Configurations)
{
Type? type = configuration.GetType().GetGenericArguments()[0];
serviceCollection.AddSingleton(typeof(IEventBuilderConfiguration<>).MakeGenericType(type), configuration);
serviceCollection.AddSingleton(typeof(EventHandler<>).MakeGenericType(type));
}
});
return hostBuilder;
}
}
}