inject the widget assembly into the IoC as we are going to need it further in

This commit is contained in:
TheXamlGuy
2024-01-27 16:21:53 +00:00
parent 48925b89ff
commit 640b3292b2
16 changed files with 105 additions and 64 deletions
+31
View File
@@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
namespace Hyperbar.Widget;
public class WidgetExtensionHandler(IProxyServiceCollection<IWidgetBuilder> typedServices,
IServiceProvider provider,
IMediator mediator) :
INotificationHandler<Created<WidgetExtension>>
{
public async Task Handle(Created<WidgetExtension> notification,
CancellationToken cancellationToken)
{
if(notification.Value is WidgetExtension widgetExtension)
{
IWidgetBuilder builder = widgetExtension.Widget.Create();
builder.ConfigureServices(args =>
{
args.AddSingleton(widgetExtension.Assembly);
args.AddTransient(_ => provider.GetRequiredService<IProxyService<IMediator>>());
args.AddRange(typedServices.Services);
});
IWidgetHost host = builder.Build();
await host.InitializeAsync();
await mediator.PublishAsync(new Created<IWidgetHost>(host),
cancellationToken);
}
}
}