[RESOLVED]GetService in NinjectDependencyResolver not getting hit

I am using a custom Dependency Resolver – NinjectDependencyResolver : IDependencyResolver.

Here is the code:

———————————–

   

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using EssentialTools.Models;
using Ninject;

namespace EssentialTools.Infrastructure
{
    public class NinjectDependencyResolver : IDependencyResolver
    {
        private IKernel kernel;

        public NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;
            AddBindings();
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }
        
        private void AddBindings()
        {
            kernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
        }
    }
}

—————————-

I registered the resolver in NinjectWebCommon. And I know my AddBindings is getting hit because I can set the breakpoint and hit and inspect. Or write debug statements to the output window.

But I don’t think my GetService is getting hit. I can’t hit the break point and I can’t write debug statements to the window.

I have set this up before and I have always been able to hit the breakpoint in GetServiceType(Type serviceType) and see that serviceType was the HomeController just to prove it was working.

I know its registered because AddBindings is working and if I comment it out the bind it errors.

Also if I comment out GetService it errors that I have not implemented it.  But even when implemented is not used.  It’s like it’s using the default.

What have I missed in the configuration?

smiller781

public IEnumerable<object> GetServices(Type serviceType)

I believe it could be using the GetServices. Try putting a breakpoint and see whether it is hitting or not.

No – its not hitting that break point either.

No break points are getting hit.

That’s the problem.

But I know its using my binding and if I comment out anything it breaks so I know its working.

Just can’t hit break points in there.

Hi smiller781,

Please share the project on the OneDrive, we will check it.

Best Regards

Starain

Ok, where is the drive?

How do I get it up there?

Thanks.

smiller781

where is the drive?

onedrive.live.com

I uploaded the project called EssentialTools.

smiller781

I uploaded the project called EssentialTools.

share and give link

https://onedrive.live.com/?cid=05DD0C84E3D90E52&id=5DD0C84E3D90E52%21114

The project is in the zip file called EssentialTools

Hi smiller,

That link is unavailable.

Please refer to this link below to share files:

http://windows.microsoft.com/en-us/onedrive/share-file-folder

Best Regards

Starain

Hi smiller781,

Base on my test, it seems not use that method.

For example, I add this code below in the GetService method to let it throw error. However it still works fine (also tried return null).

 public object GetService(Type serviceType)
        {
            string s = "gg";
            int i = int.Parse(s);
            return kernel.TryGet(null);
            //return null; 
        }

This issue is related to ninject, I suggest that you should post it to the ninject site for support.

Best Regards

Starain

Leave a Reply