1
votes

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.

1
Can you post the code of registering of the resolver?Vsevolod Goloviznin
I added what I have.kyurthich
Do you call this code at all?Vsevolod Goloviznin
Ensure that the implementation type has a public constructor Have you tried adding public before the constructor definition?Jarga
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

1 Answers

1
votes

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.