Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure Map parameterless constructor error

I am trying to set up structure map ver 3.0.5.0 with Web API 2.

I have followed this implementation: Configuring dependency injection with ASP.NET Web API 2.1

However, I am getting this error when doing a get against my ComplexesController:

An error occurred when trying to create a controller of type 'ComplexesController'. Make sure that the controller has a parameterless public constructor.

Can anyone see what is wrong with my structuremap config? The Create method never gets called.

This is my implementation:

public class StructureMapControllerActivator : IHttpControllerActivator
{
    private readonly IContainer _container;

    public StructureMapControllerActivator(IContainer container)
    {
        if (container == null) throw new ArgumentNullException("container");
        _container = container;
    }

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        try
        {                
            var scopedContainer = _container.GetNestedContainer();
            scopedContainer.Inject(typeof(HttpRequestMessage), request);
            request.RegisterForDispose(scopedContainer);
            return (IHttpController)scopedContainer.GetInstance(controllerType);
        }
        catch (Exception e)
        {
            // TODO : Logging
            throw e;
        }
    }
}

This method is in my startup...

public void InitializeContainer()
    {
        // STRUCTURE MAP
        Container container = new Container();
        GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new StructureMapControllerActivator(container));

        container.Configure(x => x.For<IForumRepository>().Use<ForumRepository>());
        container.Configure(x => x.For<IComplexRepository>().Use<ComplexRepository>());        
    }

.. and this is the controller:

 public class ComplexesController : ApiController
{

    private IComplexRepository _repo;

    public ComplexesController(IComplexRepository repo)
    {
        _repo = repo;
    }

    // GET: api/Complexes
    public IList<Complex> GetComplexes()
    {
        var complexes = _repo.GetList();
        return complexes;
    }
    ...

My full Startup class

[assembly: OwinStartup(typeof(AngularJSAuthentication.API.Startup))]
namespace AngularJSAuthentication.API
{
  public class Startup
   {
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseWebApi(config);
    }

  }
}
like image 814
Greg Avatar asked Jul 29 '14 18:07

Greg


1 Answers

The problem here is that you are registering your service activator with a GlobalConfiguration object and not your HttpConfiguration object. In this scenario The GlobalConfiguration object is never used as it is replaced by the HttpConfiguration object. In order to solve your issue you should replace your InitializeContainer() method with the following.

public void InitializeContainer(HttpConfiguration config)
    {
        // STRUCTURE MAP
        Container container = new Container();
        config.Services.Replace(typeof(IHttpControllerActivator), new StructureMapControllerActivator(container));

        container.Configure(x => x.For<IForumRepository>().Use<ForumRepository>());
        container.Configure(x => x.For<IComplexRepository>().Use<ComplexRepository>());        
    }

you should then pass the HttpConfiguration object from your Startup class to the new InitializeContainer() method.

Hope this helps.

-B

like image 64
Brendan Cutajar Avatar answered Sep 19 '22 12:09

Brendan Cutajar