【问题标题】:Ninject throws Activation Exception in a WebApi project with multiple assembliesNinject 在具有多个程序集的 WebApi 项目中引发激活异常
【发布时间】:2012-11-12 16:12:12
【问题描述】:

我的 asp.net WebApi 项目包含多个用于服务、核心和数据访问的程序集。为了在项目中使用 Ninject 作为我的 DI 容器,我从 NuGet 添加了 Ninject.Web.Common 包。然后,我将 IDependencyResolver 实现为:

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
    readonly IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel) : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(this.kernel.BeginBlock());
    }
}

public class NinjectDependencyScope : IDependencyScope
{
    IResolutionRoot resolver;

    public NinjectDependencyScope(IResolutionRoot resolver)
    {
        this.resolver = resolver;
    }

    public object GetService(System.Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        var resolved = this.resolver.Get(serviceType);
        return resolved;
    }

    public System.Collections.Generic.IEnumerable<object> GetServices(System.Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return this.resolver.GetAll(serviceType);
    }

    public void Dispose()
    {
        IDisposable disposable = resolver as IDisposable;
        if (disposable != null)
            disposable.Dispose();

        resolver = null;
    }
}

这是我的 Ninject.Web.Common.cs。

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>();
        RegisterServices(kernel);

        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(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)
    {
        kernel.Bind(x =>
            x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath)
            .SelectAllIncludingAbstractClasses()
            .BindDefaultInterface()
            .Configure(config => config.InSingletonScope()));

        //kernel.Bind(x => 
        //    {
        //        x.FromAssembliesMatching("*")
        //        .SelectAllClasses()
        //        .BindDefaultInterface()
        //        .Configure(b => b.InTransientScope());
        //    });
        //kernel.Load()
        //kernel.Bind<ISecurityService>().To<SecurityServiceImplementation>();

        //kernel.Bind(x => x
        //    .FromAssembliesMatching("*")
        //    .SelectAllClasses()
        //    .BindDefaultInterface());
        //.Configure(b => b.InTransientScope()));
        //kernel.Load("*.dll");
    }        
}

例外是

[ActivationException: Error activating IHostBufferPolicySelector
No matching bindings are available, and the type is not self-bindable.
Activation path:
1) Request for IHostBufferPolicySelector

我使用了各种注册(已注释掉),但都没有工作。 NinjectWebCommon.cs -> CreateKernel() 方法中的断点被命中,GetService(System.Type serviceType) 方法中的断点也是如此。 AppDomain.CurrentDomain.RelativeSearchPath 解析到应用程序的 bin 目录,它包含所有 dll,包括 System.Web.Http.dll,其中包含 IHostBufferPolicySelector 类型。

如何正确使用 Ninject.Extensions.Conventions 来设置内核以进行类型解析?

【问题讨论】:

    标签: c# asp.net-web-api ninject ninject-extensions ninject.web


    【解决方案1】:

    根据 Remo 的回答中的提示和 Filip 的评论以及大量的调试时间,我发现在 GetService() 实现中使用 this.resolver.Get(serviceType) 而不是 this.resolver.TryGet(serviceType) 是我的情况的罪魁祸首。

    我计划对此进行详细的博客文章,但简短的是,一旦我们将 NinjectDependencyResolver 插入到 MVC 中,使用以下行: GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); 而且我们没有定义框架级别的依赖绑定(例如 IHostBufferPolicySelector 等),当 一些 框架级别的依赖没有通过 Ninject 解析时,Get() 方法会引发异常。使用TryGet() 不会引发异常,并且框架会退回到未解决(又名空)依赖项的默认依赖项,例如 IHostBufferPolicySelector。所以,选项是

    1. 使用 TryGet() 方法解决依赖关系。
    2. 将 Get 包装在 Try/Catch 中并丢弃异常。

    【讨论】:

      【解决方案2】:

      试试这个帖子。不要捕获 Ninject 异常,而是捕获所有 WebApi 调用的异常。 http://blog.greatrexpectations.com/2013/05/15/exception-handling-for-web-api-controller-constructors/ 在堆栈跟踪中,发生异常的构造函数是可见的。

      【讨论】:

        【解决方案3】:

        没有HostBufferPolicySelector 的类,所以没有IHostBufferPolicySelector 是默认接口的类。你可以试试BindAllInterfacesBindDefaultInterfaces

        【讨论】:

        • 雷莫,谢谢你的建议。当我将 BindDefaultInterface 更改为 BindDefaultInterfaces 时,我遇到:mscorlib.dll 中发生了“System.StackOverflowException”类型的未处理异常有什么想法吗?
        • 实际上你应该更精确地定义你的约定。像所有程序集(包括所有系统程序集)中的所有类这样的约定是非常不可预测的。考虑一下您的类有哪些约定,并使用约定语法指定它们。没有约定适用的类仍然可以使用单一服务绑定语法自行绑定。
        • Remo,我可以为我自己的程序集做到这一点,而且应该这样做。但是,我不确定如何指定框架级别(asp.net、mvc、webapi)绑定,如 IHostBufferPolicySelector?有没有办法自动绑定系统/框架绑定?
        • IHostBufferPolicySelector 的默认实现是 WebHostBufferPolicySelector(不是 HostBufferPolicySelector),这就是 Ninject 无法自行找到它的原因。但是,正如 Remo 所说 - 为什么要绑定系统程序集? Web API 可以处理自己的依赖关系,你没有理由干涉它们,所以我肯定会避免通用的“绑定所有程序集中的所有类”。
        • Filip,感谢您对此进行调查。我必须在这里遗漏一些非常明显的东西,但是当我使用以下命令替换默认依赖解析器时: GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);如果没有指定任何绑定,WebApi 如何解决它的依赖关系?
        猜你喜欢
        • 1970-01-01
        • 2020-02-07
        • 2015-03-07
        • 1970-01-01
        • 2015-08-27
        • 2016-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多