Create a package for generic host
seesharper opened this issue · 1 comments
seesharper commented
Create a package for generic host
hellfirehd commented
In combination with LightInject.Microsoft.DependencyInjection
this does the job for me.
using LightInject;
using LightInject.Microsoft.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
namespace Microsoft.Extensions.DependencyInjection
{
public static class HostBuilderExtensions
{
public static IHostBuilder UseLightInject(this IHostBuilder builder, Action<ContainerOptions> action)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
var containerOptions = new ContainerOptions();
action?.Invoke(containerOptions);
return UseLightInject(builder, containerOptions);
}
public static IHostBuilder UseLightInject(this IHostBuilder builder, ContainerOptions containerOptions = null)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
containerOptions ??= new ContainerOptions();
return builder.UseServiceProviderFactory(new LightInjectServiceProviderFactory(containerOptions));
}
public static IHostBuilder UseLightInject(this IHostBuilder builder, IServiceContainer serviceContainer)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.UseServiceProviderFactory(hostBuilder => new LightInjectServiceProviderFactory(serviceContainer));
}
}
}
Then:
Host.CreateDefaultBuilder(args)
.UseLightInject()
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>())
.Build()
.Run();