Ninject和ASP.NET Web API [英] Ninject and ASP.NET Web API

查看:124
本文介绍了Ninject和ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我设置你应该知道,我是从这个页面我目前的code中的问题:
<一href=\"http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/\">http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/

我试图通过使用上面的网站上发现了一个的IDependencyResolver适配器使用的ASP.NET Web API和Ninject在我的申请。

我创建的所有code就像它显示在网站上和它的作品,但是当我加载了我的器件的应用我的正常控制器故障,并显示以下错误:

[MissingMethodException:此对象定义无参数的构造函数] 结果
[InvalidOperationException异常:试图创建类型的控制器时发生错误AccountManager.Controllers.HomeController...

所以,好像我可以与常规控制器或Web API控制器,​​但不能同时使用Ninject。 (

下面是我的code:

NinjectResolver.cs

 公共类NinjectResolver:NinjectScope,的IDependencyResolver
{
    私人的iKernel _KERNEL;    公共NinjectResolver(内核的iKernel)
        :基地(内核)
    {
        _KERNEL =内核;
    }    公共IDependencyScope BeginScope()
    {
        返回新NinjectScope(_kernel.BeginBlock());
    }
}

NinjectScope.cs

 公共类NinjectScope:IDependencyScope
{
    保护IResolutionRoot resolutionRoot;    公共NinjectScope(IResolutionRoot内核)
    {
        resolutionRoot =内核;
    }    公共对象GetService的(类型的serviceType)
    {
        IRequest请求= resolutionRoot.CreateRequest(的serviceType,空,新的参数[0],真实,真实);
        返回resolutionRoot.Resolve(要求).SingleOrDefault();
    }    公共IEnumerable的&LT;对象&gt; GetServices(类型的serviceType)
    {
        IRequest请求= resolutionRoot.CreateRequest(的serviceType,空,新的参数[0],真实,真实);
        返回resolutionRoot.Resolve(要求).ToList();
    }    公共无效的Dispose()
    {
        IDisposable的一次性=(IDisposable接口)resolutionRoot;
        如果(一次性!= NULL)disposable.Dispose();
        resolutionRoot = NULL;
    }
}

的Global.asax.cs

 公共类MvcApplication:System.Web.HttpApplication
{
    私人无效SetupDependencyInjection()
    {
        //创建Ninject DI内核
        的iKernel内核=新StandardKernel();        //注册服务,Ninject DI容器
        RegisterServices(内核);        //告诉asp.net mvc的使用我们的Ninject DI容器
        GlobalConfiguration.Configuration.DependencyResolver =新NinjectResolver(内核);
    }
}

AccountingController.cs

 公共类AccountingController:ApiController
{
    私人ICustomerService _customerService;    公共AccountingController(ICustomerService服务)
    {
        _customerService =服务;
    }    // GET / API /&LT;控制器与GT; / 5
    公共字符串GET(INT ID)
    {
        回到价值;
    }
}


解决方案

在调用之前插入下面一行code进入 CreateKernel()方法 RegisterServices(内核);

  GlobalConfiguration.Configuration.DependencyResolver =新NinjectResolver(内核);

您还需要使用低于code,I preFER有它在同一个类中定义。

 公共类NinjectResolver:NinjectScope,的IDependencyResolver
{
    私人的iKernel _KERNEL;
    公共NinjectResolver(内核的iKernel):基地(内核)
    {
        _KERNEL =内核;
    }
    公共IDependencyScope BeginScope()
    {
        返回新NinjectScope(_kernel.BeginBlock());
    }
}公共类NinjectScope:IDependencyScope
{
    保护IResolutionRoot resolutionRoot;
    公共NinjectScope(IResolutionRoot内核)
    {
        resolutionRoot =内核;
    }
    公共对象GetService的(类型的serviceType)
    {
        IRequest请求= resolutionRoot.CreateRequest(的serviceType,空,新的参数[0],真实,真实);
        返回resolutionRoot.Resolve(要求).SingleOrDefault();
    }
    公共IEnumerable的&LT;对象&gt; GetServices(类型的serviceType)
    {
        IRequest请求= resolutionRoot.CreateRequest(的serviceType,空,新的参数[0],真实,真实);
        返回resolutionRoot.Resolve(要求).ToList();
    }
    公共无效的Dispose()
    {
        IDisposable的一次性=(IDisposable接口)resolutionRoot;
        如果(一次性!= NULL)disposable.Dispose();
        resolutionRoot = NULL;
    }
}

运行它,它应该工作。这为我工作,我希望它为你。

延伸阅读:

<一个href=\"http://yassershaikh.com/using-ninject-dependency-injection-with-asp-net-web-api-controllers/\">Using Ninject - 依赖注射的ASP.NET Web API控制器

Before I set up the question you should know that I got my current code from this page: http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/

I'm trying to use ASP.NET Web API and Ninject in my application by using an IDependencyResolver adapter found on the site above.

I created all the code just like it shows on the site and it works but when I load up my appication my regular controllers fail and show this error:

[MissingMethodException: No parameterless constructor defined for this object.]
[InvalidOperationException: An error occurred when trying to create a controller of type 'AccountManager.Controllers.HomeController'...

So, it seems like I can use Ninject with regular controllers or Web API controllers but not both. :(

Here is my code:

NinjectResolver.cs

public class NinjectResolver : NinjectScope, IDependencyResolver
{
    private IKernel _kernel;

    public NinjectResolver(IKernel kernel)
        : base(kernel)
    {
        _kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectScope(_kernel.BeginBlock());
    }
}

NinjectScope.cs

public class NinjectScope : IDependencyScope
{
    protected IResolutionRoot resolutionRoot;

    public NinjectScope(IResolutionRoot kernel)
    {
        resolutionRoot = kernel;
    }

    public object GetService(Type serviceType)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).SingleOrDefault();
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).ToList();
    }

    public void Dispose()
    {
        IDisposable disposable = (IDisposable)resolutionRoot;
        if (disposable != null) disposable.Dispose();
        resolutionRoot = null;
    }
}

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    private void SetupDependencyInjection()
    {
        //create Ninject DI Kernel
        IKernel kernel = new StandardKernel();

        //register services with Ninject DI container
        RegisterServices(kernel);

        //tell asp.net mvc to use our Ninject DI Container
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
    }
}

AccountingController.cs

public class AccountingController : ApiController
{
    private ICustomerService _customerService;

    public AccountingController(ICustomerService service)
    {
        _customerService = service;
    }

    // GET /api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }
}

解决方案

Insert the following line of code into the CreateKernel() method before the call to the RegisterServices(kernel); is made.

GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);

You will also need to use the below code, I prefer to have it defined in the same class.

public class NinjectResolver : NinjectScope, IDependencyResolver
{
    private 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)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).SingleOrDefault();
    }
    public IEnumerable<object> GetServices(Type serviceType)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).ToList();
    }
    public void Dispose()
    {
        IDisposable disposable = (IDisposable)resolutionRoot;
        if (disposable != null) disposable.Dispose();
        resolutionRoot = null;
    }
}

Run it, and it should work. This worked for me, I hope it does for you too.

Further Reading :

Using Ninject – Dependency Injection with ASP.NET Web API controllers

这篇关于Ninject和ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆