Available on Nuget here: https://www.nuget.org/packages/StructureMap.Microsoft.DependencyInjection.Forked/
This was forked from this underlying library due to this bug around Disposing objects.
Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new StructureMapContainerBuilderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void ConfigureContainer(Container builder)
{
builder.Configure(config =>
{
// Your services here
config.AddRegistry(new MyRegistry());
});
}
The registry:
public class MyRegistry : Registry
{
public MyRegistry()
{
For<Something>().Singleton().Use<Something>();
}
}
StructureMapContainerBuilderFactory.cs
public class StructureMapContainerBuilderFactory : IServiceProviderFactory<Container>
{
private IServiceCollection _services;
public Container CreateBuilder(IServiceCollection services)
{
_services = services;
return new Container();
}
public IServiceProvider CreateServiceProvider(Container builder)
{
builder.Configure(config =>
{
config.Populate(_services);
});
return builder.GetInstance<IServiceProvider>();
}
}