Saturday, November 4, 2017

I am pleased to report that it is easy to get StructureMap working with a .NET Core application.

Steps:

  1. I ran this command in the NuGet Console in Visual Studio 2017...
    install-package StructureMap
    ...which gave me StructureMap 4.5.2.
     
  2. I followed what was suggested at this to add...
    ConfigureIoC(services);
    ...immediately after...
    services.AddMvc();
    ...in the ConfigureServices method inside of Startup.cs.
     
  3. The line of code I added was red/angry as it tried to find a method that wasn't there. I put this method into Startup.cs to fix that:
    public void ConfigureIoC(IServiceCollection services)
    {
       services.AddTransient<IFlatFileMechanics, FlatFileMechanics>();
    }

 
 

Startup.cs ends up looking like so:

using HelloWorld.Core;
using HelloWorld.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace HelloWorld.RestApi
{
   public class Startup
   {
      public Startup(IHostingEnvironment env)
      {
         var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
         Configuration = builder.Build();
      }
      
      public IConfigurationRoot Configuration { get; }
      
      public void ConfigureServices(IServiceCollection services)
      {
         services.AddMvc();
         ConfigureIoC(services);
      }
      
      public void Configure(IApplicationBuilder app, IHostingEnvironment env,
            ILoggerFactory loggerFactory)
      {
         loggerFactory.AddConsole(Configuration.GetSection("Logging"));
         loggerFactory.AddDebug();
         app.UseMvc();
      }
      
      public void ConfigureIoC(IServiceCollection services)
      {
         services.AddTransient<IFlatFileMechanics, FlatFileMechanics>();
      }
   }
}

 
 

With modern StructureMap we expect dependencies to be hydrated at controller constructor signatures and that is still true. This worked for me:

using HelloWorld.Core;
using Microsoft.AspNetCore.Mvc;
namespace HelloWorld.RestApi.Controllers
{
   [Route("api/[controller]")]
   public class ValuesController : Controller
   {
      public IFlatFileMechanics _flatFileMechanics;
      
      public ValuesController(IFlatFileMechanics flatFileMechanics)
      {
         _flatFileMechanics = flatFileMechanics;
      }
      
      [HttpGet]
      public string Get()
      {
         return _flatFileMechanics.ReadFlatFile();
      }
   }
}

 
 

Addendum 10/3/2018: If you look close we are not using StructureMap at all. The IoC is just a part of .NET Core now. We don't need to explicitly use StructureMap. I was misguided when I typed this up.

No comments:

Post a Comment