StructureMap support for Microsoft.Extensions.DependencyInjection
Switch branches/tags
Clone or download
Latest commit 0478649 Aug 25, 2017

README.md

StructureMap integration for ASP.NET Core Build status

This repository contains the source of two NuGet packages:

  • StructureMap.AspNetCore
  • StructureMap.Microsoft.DependencyInjection (formerly known as StructureMap.Dnx)

These packages provide integration with ASP.NET Core and the built-in container on different levels.

StructureMap.AspNetCore

Adds integration with the ASP.NET Core hosting mechanism.

⚠️ For this integration package to work properly, you need version 1.1 of ASP.NET Core

Installation

Add StructureMap.AspNetCore to your project:

<ItemGroup>
  <PackageReference Include="StructureMap.AspNetCore" Version"<version>" />
</ItemGroup>

Usage

The package adds the UseStructureMap extension method to IWebHostBuilder. Calling this method will instruct the ASP.NET Core host to create a StructureMap Registry and optionally let the user configure it using a Startup.ConfigureContainer(Registry) method.

Example

using System.IO;
using Microsoft.AspNetCore.Hosting;
using StructureMap.AspNetCore;

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .UseIISIntegration()
            .UseStructureMap()
            .UseKestrel()
            .Build();

        host.Run();
    }
}

The runtime will then look for a ConfigureContainer method on the specified Startup class:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Configure the ASP.NET specific stuff.
    }

    public void ConfigureContainer(Registry registry)
    {
        // Use StructureMap-specific APIs to register services in the registry.
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    }
}

StructureMap.Microsoft.DependencyInjection

Adds StructureMap support for Microsoft.Extensions.DependencyInjection

Installation

Add StructureMap.Microsoft.DependencyInjection to your project:

<ItemGroup>
  <PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version"<version>" />
</ItemGroup>

Usage

The package contains a single, public extension method, Populate. It's used to populate a StructureMap container using a set of ServiceDescriptors or an IServiceCollection.

Example

using System;
using Microsoft.Extensions.DependencyInjection;
using StructureMap;

public class Startup
{
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddWhatever();

        var container = new Container();
        
        // You can populate the container instance in one of two ways:
        
        // 1. Use StructureMap's `Configure` method and call
        //    `Populate` on the `ConfigurationExpression`.
        
        container.Configure(config =>
        {
            // Register stuff in container, using the StructureMap APIs...

            config.Populate(services);
        });
        
        // 2. Call `Populate` directly on the container instance.
        //    This will internally do a call to `Configure`.
        
        // Register stuff in container, using the StructureMap APIs...

        // Here we populate the container using the service collection.
        // This will register all services from the collection
        // into the container with the appropriate lifetime.
        container.Populate(services);

        // Finally, make sure we return an IServiceProvider. This makes
        // ASP.NET use the StructureMap container to resolve its services.
        return container.GetInstance<IServiceProvider>();
    }
}