相关疑难解决方法(0)

Ninject在具有多个程序集的WebApi项目中抛出Activation Exception

我的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) …
Run Code Online (Sandbox Code Playgroud)

c# ninject ninject.web ninject-extensions asp.net-web-api

5
推荐指数
1
解决办法
7807
查看次数