StructureMap support for Microsoft.Extensions.DependencyInjection
C# PowerShell CSS JavaScript
Latest commit ade0da9 Dec 19, 2016 @khellang khellang committed on GitHub Update README.md

README.md

StructureMap integration for ASP.NET Core Build status

This repository contains the source of two NuGet packages:

  • StructureMap.AspNetCore
  • StructureMap.Microsoft.DependencyInjection

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.

Installation

Add StructureMap.AspNetCore to your project.json:

"dependencies": {
  "StructureMap.AspNetCore": "<version>"
}

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;

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

(Formerly known as StructureMap.Dnx)

Adds StructureMap support for Microsoft.Extensions.DependencyInjection

Installation

Add StructureMap.Microsoft.DependencyInjection to your project.json:

"dependencies": {
  "StructureMap.Microsoft.DependencyInjection": "<version>"
}

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