注射依赖转换为Web API控制器 [英] Injecting Dependency into Web API Controller

查看:226
本文介绍了注射依赖转换为Web API控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要注入团结容器放入WebController。

I want to inject unity container into WebController.

我有UnityDependencyResolver:

I have UnityDependencyResolver:

public class UnityDependencyResolver : IDependencyResolver
{
    readonly IUnityContainer _container;

    public UnityDependencyResolver(IUnityContainer container)
    {
    this._container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return _container.Resolve(serviceType);
        }
        catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _container.ResolveAll(serviceType);
        }
        catch
        {
            return new List<object>();
        }
    }

    public void Dispose()
    {
        _container.Dispose();
    }
}

然后,在我的Global.asax我添加了以下行:

Then, in my Global.asax I add the following line:

var container = new UnityContainer();
container.RegisterType<IService, Service>
(new PerThreadLifetimeManager()).RegisterType<IDALContext, DALContext>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

然后,如果我用下面的在Web控制器:

Then, If I use the following in a Web Controller:

private IService _service;

public HomeController(IService srv)
{
    _service = srv;
}

它工作正常。

但我想它注入的WebAPI控制器,​​所以如果我做同样的方式:

But I want to inject it into WebAPI Controller, so if I do it the same way:

private IService _service;

public ValuesController(IService srv)
{
    _service = srv;
}

这是不行的,它说,构造函数没有定义。
好吧,我再创建一个构造函数:

It does not work, it says that constructor is not defined. Ok, I create one more constructor:

public ValuesController(){}

而在这种情况下,仅使用此构造,从来没有一个,我应该团结注入容器。

And in this case it uses only this constructor and never the one where I should inject unity container.

请指教。

推荐答案

在您的WebApiConfig补充一点:

Add this in your WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Routes and other stuff here...

        var container = IocContainer.Instance; // Or any other way to fetch your container.
        config.DependencyResolver = new UnityDependencyResolver(container);
    }
}

如果你想在同一容器你可以把它在一个静态变量,像这样:

And if you want the same container you can keep it in a static variable, like so:

public static class IocContainer
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        return container;
    });

    public static IUnityContainer Instance
    {
        get { return Container.Value; }
    }
}

更多信息可以在这里找到:

More info can be found here:

http://www.asp.net/web-api/概述/高级/依赖注入

在一个旁注,我也可以推荐的NuGet包 Unity.Mvc 。它增加了一个 UnityWebActivator 和支持 PerRequestLifetimeManager

On a sidenote, I can also recommend the nuget-package Unity.Mvc. It adds a UnityWebActivator and support for PerRequestLifetimeManager.

https://www.nuget.org/packages/Unity.Mvc/

这篇关于注射依赖转换为Web API控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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