NinjectDependencyResolver fails binding ModelValidatorProvider

落爺英雄遲暮 提交于 2019-11-28 06:49:53

I was having all sorts of grief with the WebApi2 and Ninject initialization after upgrading the Ninject packages (even uninstalling and deleting the old ones).

Specifically in your case I would remove these lines of code:

// Use the container and our NinjectDependencyResolver as
// application's resolver
var resolver = new NinjectDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;

as they are probably the cause of the error (the NinjectWebCommon.cs and Ninject libraries deal with initializing the dependency resolver now).


For others out there who followed a similar upgrade path to me. What worked for me was the following:

  • Remove the old DependencyResolver initialization code (for me this was causing the specific error you mention as in earlier versions of Ninject/WebApi2, putting these lines in the WebApiConfig.cs Register() method was how you initialized the DependencyResolver...this no longer is the case):

    var kernel = new StandardKernel();
    config.DependencyResolver = new NinjectDependencyResolver(kernel);
    
  • Install the Ninject.Web.WebApi.WebHost package. This installed the NinjectWebCommon.cs file. For me, just having the Ninject.Web.WebApi and it's dependencies didn't create this file.

My installed and working Ninject Packages for reference:

<package id="Ninject" version="3.2.2.0" targetFramework="net452" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net452" />
<package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net452" />
<package id="Ninject.Web.WebApi" version="3.2.3.0" targetFramework="net452" />
<package id="Ninject.Web.WebApi.WebHost" version="3.2.3.0" targetFramework="net452" />

Be sure that any old Ninject or Ninject.Web.Common.* dlls aren't present in your bin folder.

I had the same issue in my solution after I had uninstalled Ninject.Web.Common, Ninject.Web.Common.WebHost, Ninject.Web.WebApi, and Ninject.MVC5 from Nuget and installed WebApiContrib.IoC.Ninject in order to use GlobalConfiguration.Configuration.DependencyResolver as in your example. I kept the version of Ninject that I already had installed (which was indeed 3.2.2).

The error did not appear when I first made my changes. However, after moving around from a few git branches and back to my current work, I saw the error. The code that had run fine last week was now throwing the same exact error.

It seems that my bin folder had references to the old Ninject.* packages I had removed. Upon deleting those files, my project worked as expected.

The cyclic dependency is between the classes "NinjectDefaultModelValidatorProvider" and "DefaultModelValidatorProviders".Simply add a binding for "DefaultModelValidatorProviders" on your startup like below:

_kernel.Bind<DefaultModelValidatorProviders>().ToConstant(new DefaultModelValidatorProviders(config.Services.GetServices(typeof (ModelValidatorProvider)).Cast<ModelValidatorProvider>()));

In my case it was working just fine in Owin Selfhost context, but not when hosted in IIS. My solution was to remove all Ninject related assemblies from nuget packages except Ninject itself.

Then I wrote my own DependencyResolver class, feel free to leave improvements in the comments.

public class NinjectDepsolver : IDependencyResolver
{
    private IKernel _kernel;

    public NinjectDepsolver(IKernel kernel)
    {
        _kernel = kernel;
    }

    public void Dispose()
    {
        _kernel = null;
    }

    public object GetService(Type serviceType) => _kernel.TryGet(serviceType);

    public IEnumerable<object> GetServices(Type serviceType) => _kernel.GetAll(serviceType).ToArray();

    public IDependencyScope BeginScope() => new DepScope(this);

    class DepScope : IDependencyScope
    {
        private NinjectDepsolver _depsolver;

        public DepScope(NinjectDepsolver depsolver)
        {
            _depsolver = depsolver;
        }

        public void Dispose()
        {
            _depsolver = null;
        }

        public object GetService(Type serviceType) => _depsolver.GetService(serviceType);

        public IEnumerable<object> GetServices(Type serviceType) => _depsolver.GetServices(serviceType);
    }
}

And then in your Owin Configuration method:

var kernel = new StandardKernel();
kernel.Load(<your module classes>);
var httpConfig = new HttpConfiguration();
var httpConfig.DependencyResolver = new NinjectDepsolver(kernel);
var httpConfig.MapHttpAttributeRoutes();

app.UseWebApi(httpConfig);

This is what worked for me.

uninstall-package Ninject.Web.WebApi.WebHost

The above command uninstalled the version 'Ninject.Web.WebApi.WebHost 3.2.4.0' and the error is gone!!

Just reconfirm, I have installed the same package using the command

install-package Ninject.Web.WebApi.WebHost

and the command installed the package 'Ninject.Web.WebApi.WebHost 3.2.4.0' and the error reappeared.

Bhanu Bhanot
var _surveyBusiness = _kernel.Get<ISurveyBusiness>();
_surveyBusiness.SomeFunc(user.CompanyId, user.UserId);

This is working also.

I fixed this by adding the following line in Global.asax (where my StandardKernel was being initialized):

kernel.Bind<DefaultModelValidatorProviders>().ToConstant(new DefaultModelValidatorProviders(GlobalConfiguration.Configuration.Services.GetModelValidatorProviders()));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!