如何使用会话值与团结DependencyResolver [英] How to use session values with Unity and DependencyResolver

查看:115
本文介绍了如何使用会话值与团结DependencyResolver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC4和Unity 2.1。我的服务需要根据从会话状态检索凭据服务密钥。

I'm using MVC4 and Unity 2.1. My services require a service key based on credentials retrieved from session state.

我喜欢这样我的注册服务(S):

I register my service(s) like so:

container.RegisterType<IInventoryService, InventoryService>();

有关InventoryService的构造也同样简单:

The constructor for InventoryService is equally simple:

public InventoryService(ServiceKey serviceKey) {  ...  }

在我的网站,当我需要我使用一个服务定位器,可以自动组成利用会话证书服务密钥的服务。

In my website when I've needed a service I use a service locator that automatically composes the service key using credentials from session.

public static T Resolve<T>(ServiceKey serviceKey = null)
    {
        if (serviceKey == null)
        {
            serviceKey = SessionManager.ServiceKey;
        }

        var parameterOverride = new ParameterOverride(SERVICEKEY_PARAMETERNAME, serviceKey);

        return Resolve<T>(null, parameterOverride);
    }

这一直运作良好。问题是,我在我的网站现在转换为MVC和尝试中注入服务使用使用我退出服务定位器(依赖工厂)一个简单的依赖解析器控制器:

This has worked well. The problem is that I'm now converting my site to MVC and attempting to inject services into controllers using a simple dependency resolver that uses my exiting service locator (dependency factory):

public class CustomDependencyResolver : IDependencyResolver
{
    public object GetService(Type serviceType)
    {
        return MvcDependencyFactory.Resolve(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return MvcDependencyFactory.ResolveAll(serviceType);
    }
}

我的控制器是这样的:

My controller looks like:

public InventoryController(IInventoryService inventoryService) { ... }

问题是,MVC仍然抱怨试图实例库存控制器时,大约没有找到一个参数的构造函数。我想这是因为我还没有登记在Unity服务密钥。但如果我尝试这样做,我觉得MVC正试图解决控制器,以及随后的服务,会议甚至被前建成。

The problem is that MVC still complains about not finding a parameterless constructor when trying to instantiate the inventory controller. I think this is because I haven't registered a service key in Unity. But if I try doing so, I find that MVC is trying to resolve the controllers, and subsequently the services, before session has even been constructed.

我这不是想这个正确?每一步都感觉pretty合理 - 在服务中使用会话证书,在控制器使用服务,使用解析,以帮助建立控制器 - 但我一直在打我的头撞在墙上得到这个工作。

Am I not thinking about this correctly? Each step feels pretty reasonable -- using session credentials in a service, using a service in a controller, using a resolver to help build the controller -- but I've been beating my head against the wall getting this to work.

推荐答案

您可以使用在Unity(Microsoft.P​​ractices.Unity.InjectionFactory)的InjectionFactory指定一个函数来处理你的依赖的分辨率。当依赖性被解析这个功能才会执行。在下面的例子中,C是作为参数传递,这样就可以在函数中做更多的解决了您的团结容器。

You can use the InjectionFactory in Unity (Microsoft.Practices.Unity.InjectionFactory) to specify a function to handle the resolution of your dependency. This function will only be executed when the dependency is resolved. In the below example, "c" is your Unity container passed as a argument so that you can do additional resolves within your function.

替换:

container.RegisterType<IInventoryService, InventoryService>();

container.RegisterType<IInventoryService>(new InjectionFactory(c =>
    new InventoryService(SessionManager.ServiceKey)));

这篇关于如何使用会话值与团结DependencyResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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