NinjectDependencyResolver fails binding ModelValidatorProvider

前端 未结 10 1517
迷失自我
迷失自我 2020-12-08 14:00

I\'m developing an ASP.NET Web Api 2.2 with C#, .NET Framework 4.5.1.

After updating my Web.Api to Ninject 3.2.0 I get this error:

Error activating M         


        
相关标签:
10条回答
  • 2020-12-08 14:36

    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.

    0 讨论(0)
  • 2020-12-08 14:37
    var _surveyBusiness = _kernel.Get<ISurveyBusiness>();
    _surveyBusiness.SomeFunc(user.CompanyId, user.UserId);
    

    This is working also.

    0 讨论(0)
  • 2020-12-08 14:40

    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.

    0 讨论(0)
  • 2020-12-08 14:41

    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()));
    
    0 讨论(0)
  • 2020-12-08 14:42

    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>()));
    
    0 讨论(0)
  • 2020-12-08 14:51

    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);
    
    0 讨论(0)
提交回复
热议问题