Reputation: 709
Since migrating my WebApi project to VS2015 from VS2013, I can't get my dependency injection to work. I'm using StructureMap. I'm getting an error that contains this exception message: "An error occurred when trying to create a controller of type 'SearchListController'. Make sure that the controller has a parameterless public constructor." This used to work. Can anyone tell me what I'm doing wrong? Please forgive me if I have not written this question correctly. I think this is the first question I've posted here.
namespace SearchWebAPI.DependencyResolution
{
using StructureMap;
public static class IoC
{
public static IContainer Initialize()
{
return new Container(c =>
{
c.AddRegistry<DefaultRegistry>();
});
}
}
}
namespace SearchWebAPI.DependencyResolution {
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
using SearchWebAPI.Models;
public class DefaultRegistry : Registry {
#region Constructors and Destructors
public DefaultRegistry() {
Scan(
scan => {
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<ISearchListRepository>().Use<SearchListRepository>();
}
#endregion
}
}
Upvotes: 0
Views: 4313
Reputation: 709
I have this working now. Here's my code:
namespace SearchWebAPI.DependencyResolution {
using StructureMap;
public static class IoC {
public static IContainer Initialize() {
return new Container(c => c.AddRegistry<DefaultRegistry>());
}
}
}
namespace SearchWebAPI.DependencyResolution
{
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
using SearchWebAPI.Models;
public class DefaultRegistry : Registry
{
#region Constructors and Destructors
public DefaultRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
}
#endregion
}
}
namespace SearchWebAPI
{
using SearchWebAPI.DependencyResolution;
using StructureMap;
using System.Web.Http;
using System.Web.Mvc;
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
App_Start.StructuremapMvc.Start();
//StructureMap Container
IContainer container = IoC.Initialize();
//Register for MVC
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
//Register for Web API
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
}
}
}
Two lines of code fixed my problem:
//Register for MVC
DependencyResolver.SetResolver(new StructureMapDependencyResolve(container));
//Register for Web API
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
Upvotes: 3
Reputation: 9281
The error you're seeing (An error occurred when trying to create a controller of type 'SearchListController') is the result of StructureMap being unable to resolve the dependencies in your controller and is usually coupled.
Having looked at your code it's hard to see where the problem lies. How have you configured StructureMap with your ASP.NET application? (via your own controller factory or via MVC's DependencyResolver
?), and can you provide more of your code?
Also, using StructureMaps's AssertConfigurationIsValid();
is a great way to debug problems. You can call it like so:
var container = new Container();
container.AssertConfigurationIsValid();
Upvotes: 2