1

I have no idea what is going on with this. It makes no sense to me.

I have a controller that throws the following error:

System.InvalidOperationException: An error occurred when trying to create a controller of type 'LandingController'. Make sure that the controller has a parameterless public constructor. ---> Ninject.ActivationException: Error activating IApiService using binding from IApiService to ApiService No constructor was available to create an instance of the implementation type. Activation path: 2) Injection of dependency IApiService into parameter apiService of constructor of type LandingController 1) Request for LandingController Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

No matter what I do nothing works. If I have:

  • no constructors in the controller
  • one constructor with the service
  • two constructors with the service and parameterless

If I hope for the parameterless constructor to work, then it does not resolve the IApiService.

I have the following setup in NinjectWebCommon:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IApiService>().To<ApiService>();
    kernel.Bind<IMembersClient>().To<MembersClient>();
}  

Controller is:

public class LandingController : Controller
{

    IApiService _apiService;

    LandingController(IApiService apiService)
    {
        _apiService = apiService;
    }

    // GET: Landing

    public ActionResult Index()
    {
        var avm = new ApplicationViewModel();


        _apiService.GetAcc();


        return View(avm);
    }
}

API Service is:

public class ApiService : IApiService
{
    private readonly IMembersClient _membersClient;

    ApiService(IMembersClient membersClient)
    {
        _membersClient = membersClient;
    }

    public void GetAcc()
    {
        _membersClient.Test();
    }
}

Member Client is:

public class MembersClient : IMembersClient 
{
    public MembersClient()
    {
      public void Test()
      {

      }
    }
}

This was the best post I found:

Ninject Dependency Injection with Asp.Net MVC3 or MVC4

But it never helped solve the issue.

EDIT: Full NinjectWebCommon

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IApiService>().To<ApiService>();
        kernel.Bind<IMembersClient>().To<MembersClient>();
    }

EDIT : Trying Property Injection

Code for property injection:

[Inject]
public IApiService ApiServiceC { private get; set; }

Updated Error:

System.InvalidOperationException: An error occurred when trying to create a controller of type 'LandingController'. Make sure that the controller has a parameterless public constructor. ---> Ninject.ActivationException: Error activating IApiService using binding from IApiService to ApiService No constructor was available to create an instance of the implementation type. Activation path: 2) Injection of dependency IApiService into property ApiServiceC of type LandingController 1) Request for LandingController Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

Community
  • 1
  • 1
kyurthich
  • 100
  • 1
  • 11
  • 1
    Can you post the code of registering of the resolver? – Vsevolod Goloviznin Jan 29 '15 at 21:17
  • I added what I have. – kyurthich Jan 29 '15 at 21:19
  • Do you call this code at all? – Vsevolod Goloviznin Jan 29 '15 at 21:20
  • 3
    `Ensure that the implementation type has a public constructor` Have you tried adding `public` before the constructor definition? – Jarga Jan 29 '15 at 21:20
  • Yep. Did not make a difference. – kyurthich Jan 29 '15 at 21:20
  • Did you try adding a parameterless constructor? – Jarga Jan 29 '15 at 21:22
  • Yeah then it just uses that and then IApiService never resolves. – kyurthich Jan 29 '15 at 21:22
  • Also! Ninject works fine everywhere else. It is just an issue in controllers. – kyurthich Jan 29 '15 at 21:26
  • 1
    Please debug your code and make sure that (1) your method `RegisterServices()` is called and (2) that the creation of the `LandingController` is done via the exactly same kernel that was given to the `RegisterServices()` method. Can your confirm (really confirm) this is the case? – Quality Catalyst Jan 29 '15 at 21:27
  • DI is implemented correctly under the assumption the namespaces are used in the correct way and there is no clash of classes with the same name. Do you have anywhere classes lying around that are of the same name (different namespaces) such as for testing? – Quality Catalyst Jan 29 '15 at 21:31
  • Where is RegisterServices() invoked? Is it even invoked anywhere? There should be some bootstrapping code (probably inside App_Start). – Matt Jan 29 '15 at 21:32
  • `RegisterServices` is called and from what I checked the creation of `LandingController` looks to be fine. I do not currently have anything else named related to the `LandingController` – kyurthich Jan 29 '15 at 21:34
  • `RegisterServices` is invoked in `NinjectWebCommon` I added that code to the bottom of my post. – kyurthich Jan 29 '15 at 21:35
  • As a test try to manually resolve controller (i.e. next to registration) and capture exception information - there is a good chance that you are missing some dependencies. Note that ASP.Net MVC factory will *ignore* all useful resolution exceptions and try default (where you get the error from). – Alexei Levenkov Jan 29 '15 at 21:54
  • Thanks I will try that. I also tried property injection, which is at the bottom of my post. – kyurthich Jan 29 '15 at 21:55
  • Do you have any custom Controller Factories registered? Do you infact have the Ninject.Web.Common nuget package installed? Make sure you do NOT have a NinjectHttpApplication derived HttpApplication. – Erik Funkenbusch Jan 29 '15 at 22:23
  • Try reversing the order of kernel.Bind. I mean MembersClient first and ApiService second. – Kiran Varsani Jan 29 '15 at 23:44
  • I tried reversing them. Weird thing is I have another service at the same level and that one works just fine on the same controller. ApiService does not. – kyurthich Jan 30 '15 at 14:13
  • It is something to do with the injection of the IMembersClient in the ApiService. If I remove it ... it works fine. – kyurthich Jan 30 '15 at 14:18
  • Looks like anything getting injected into the ApiService has issues. – kyurthich Jan 30 '15 at 14:34
  • I completely deleted ApiService and IApiService and recreated them... and everything started working. That sucks... but I am glad it is working. – kyurthich Jan 30 '15 at 14:47

1 Answers1

1

Well.... after much testing and trying different things.

The solution was to delete IApiService and ApiService completely and recreate them.

That successfully made everything wire up correctly again.

kyurthich
  • 100
  • 1
  • 11