Skip to content

dustinsoftware/StructureMap.Microsoft.DependencyInjection

 
 

Repository files navigation

.NET Core 3.1 Structuremap Integration

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.

Integration guide

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>();
    }
}

About

StructureMap integration for ASP.NET Core

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%