简体   繁体   English

Ninject.ActivationException:激活IMainLicense时出错

[英]Ninject.ActivationException: Error activating IMainLicense

I don't know fully how Ninject works that's why I'am asking this question here to figure out what's wrong. 我不完全知道Ninject是如何工作的,这就是为什么我在这里问这个问题来弄清楚什么是错的。

If i create a empty constructor in ClaimsSecurityService it gets hit. 如果我在ClaimsSecurityService中创建一个空构造函数,它就会被命中。

This is my error: 这是我的错误:

Error activating IMainLicense No matching bindings are available, and the type is not self-bindable. 激活IMainLicense时出错没有可用的匹配绑定,并且该类型不可自我绑定。 Activation path: 激活路径:

3) Injection of dependency IMainLicense into parameter mainLicenses of constructor of type ClaimsSecurityService 3)将依赖关系IMainLicense注入到ClaimsSecurityService类型的构造函数的参数mainLicenses中

2) Injection of dependency ISecurityService into parameter securityService of constructor of type AccountController 2)将依赖关系ISecurityService注入到AccountController类型的构造函数的参数securityService中

1) Request for AccountController 1)请求AccountController

Stack: 堆:

Ninject.KernelBase.Resolve(IRequest request) +474
   Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) +153
   Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) +747
   Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) +269
   Ninject.Activation.Providers.<>c__DisplayClass4.<Create>b__2(ITarget target) +69
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +66
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +216
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +77
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +847
   Ninject.Activation.Context.ResolveInternal(Object scope) +218
   Ninject.Activation.Context.Resolve() +277
   Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4059897
   Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) +169
   Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) +747
   Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) +269
   Ninject.Activation.Providers.<>c__DisplayClass4.<Create>b__2(ITarget target) +69
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +66
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +216
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +77
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +847
   Ninject.Activation.Context.ResolveInternal(Object scope) +218
   Ninject.Activation.Context.Resolve() +277
   Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4059897
   Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType) +145
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +87

[InvalidOperationException: An error occurred when trying to create a controller of type 'Successful.Struct.Web.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +257
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +157
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Account controller: 账户管理员:

public class AccountController : Controller
{
    private readonly ISecurityService _securityService;

    public AccountController(ISecurityService securityService)
    {
        _securityService = securityService;
    }

    //
    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }
}

NinjectWebCommon: NinjectWebCommon:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Dependencies;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Extensions.Conventions;
using Ninject.Parameters;
using Ninject.Syntax;
using Ninject.Web.Common;
using Successful.Struct.Web;

[assembly: WebActivator.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]

namespace Successful.Struct.Web
{
    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();

            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            kernel.Load("Successful*.dll");
            kernel.Bind(x => x.FromAssembliesMatching("Successful*.dll")
                  .SelectAllClasses()
                  .BindAllInterfaces()
                  );
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
            RegisterServices(kernel);
            return kernel;

        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
        }
    }

    public class NinjectResolver : NinjectScope, IDependencyResolver
    {
        private readonly IKernel _kernel;
        public NinjectResolver(IKernel kernel)
            : base(kernel)
        {
            _kernel = kernel;
        }
        public IDependencyScope BeginScope()
        {
            return new NinjectScope(_kernel.BeginBlock());
        }
    }

    public class NinjectScope : IDependencyScope
    {
        protected IResolutionRoot ResolutionRoot;
        public NinjectScope(IResolutionRoot kernel)
        {
            ResolutionRoot = kernel;
        }
        public object GetService(Type serviceType)
        {
            var request = ResolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return ResolutionRoot.Resolve(request).SingleOrDefault();
        }
        public IEnumerable<object> GetServices(Type serviceType)
        {
            var request = ResolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return ResolutionRoot.Resolve(request).ToList();
        }
        public void Dispose()
        {
            var disposable = (IDisposable)ResolutionRoot;
            if (disposable != null) disposable.Dispose();
            ResolutionRoot = null;
        }
    }

}

ClaimsSecurityService: ClaimsSecurityService:

public class ClaimsSecurityService : ISecurityService
    {
        private const string AscClaimsIdType = "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider";
        private const string SuccessfulStructWebNamespace = "Successful.Struct.Web";

        private readonly IMainLicense _mainLicenses;
        private readonly ICompany _companys;
        private readonly IAuthTokenService _authService;

        [Inject]
        public IApplicationContext ApplicationContext { get; set; }
        [Inject]
        public ILogger<LocationService> Logger { get; set; }

        public ClaimsSecurityService(IMainLicense mainLicenses, ICompany companys, IAuthTokenService authService)
        {
            _mainLicenses = mainLicenses;
            _companys = companys;
            _authService = authService;
        }
}

NInject isn't quite completely automagical; NInject不是完全自动化的; you still need to tell it sometimes which implementations of your interfaces to create and inject (your "bindings" in NInject terms). 你仍然需要告诉它有时创建和注入接口的哪些实现(你在NInject术语中的“绑定”)。 Here is where they belong: 这是他们所属的地方:

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        // halp!
    }

You should set up bindings for each of the services you're attempting to inject into your classes and controllers, in the proper scope. 您应该在适当的范围内为您尝试注入类和控制器的每个服务设置绑定。 For example: 例如:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IMainLicense>().To<MainLicense>();
    }

Here's a sample from one of my projects: 这是我的一个项目的样本:

    private static void RegisterServices(IKernel kernel)
    {
        // Authentication
        kernel.Bind<IAuthenticationService>().To<AuthenticationService>().InRequestScope();

        // Entity Framework Unit of Work
        kernel.Bind<IUnitOfWork>().To<UnitOfWork>()
            .InRequestScope()
            .WithConstructorArgument("context", f => new DbContext("BlahBlahEntities"));

        // Document generator service
        kernel.Bind<IDocumentGeneratorService>().To<DummyDocumentGeneratorService>().InRequestScope();

        // Loggers
        kernel.Bind<ILog>().ToMethod(p => LogManager.GetLogger(p.Request.Target.Member.DeclaringType));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM