Search code examples
asp.net.netsimple-injector

Getting parameterles public constructor error


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

at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) ↵ at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) ↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()

When I try to reach my IFeedbackRepository I get the error aboe. It happens when I put in the constructor in my ChatBotController.cs

public class ChatBotController : ApiController
{
    IFeedbackRepository _feedbackRepository;
    public ChatBotController(IFeedbackRepository feedbackRepository)
    {
        _feedbackRepository = feedbackRepository;
    }

    [HttpPost]
    public IHttpActionResult PostQuestion([FromBody]string message) //TODO: make sure that webapi will search the message in the body of the http request
    {
        throw new NotImplementedException();
    }
}

I'm using both MVC and Api which I both resolve in my Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    DependencyConfig.RegisterWebApiDependencies();
    DependencyConfig.RegisterMvcDependencies();
}

This is my DependencyConfig.cs for both MVC and Api:

public static void RegisterWebApiDependencies()
{
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

    container.Register<IAnswerGenerator, PxlAnswerGenerator>(Lifestyle.Scoped);
    container.Register<ChatBotDbContext>(Lifestyle.Scoped);
    container.Register<IFeedbackRepository, FeedbackDbRepository>(Lifestyle.Scoped);

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

}

public static void RegisterMvcDependencies()
{
    var container = new Container();

    container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

    container.Register<IFeedbackRepository, FeedbackDbRepository>(Lifestyle.Scoped);
    container.Register<ChatBotDbContext>(Lifestyle.Scoped);

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}

What am I doing wrong?


Solution

  • According to the documentation of Simple-Injector when you want to initialize the resolver for the WebApi part of your registration you need to call

    container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
    container.Verify();
    DependencyResolver.SetResolver(new SimpleInjectorWebApiDependencyResolver(container));