如何配置依赖注入的ASP.NET MVC 3 RTM [英] How to configure dependency injection with ASP.NET MVC 3 RTM

查看:195
本文介绍了如何配置依赖注入的ASP.NET MVC 3 RTM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级从ASP.NET 3 preVIEW 1到RTM web应用程序,我由更新的方法迷惑依赖注入。我使用StructureMap为此但是这不是我的问题确实有关。 previously所有我需要做的是如下:

I am upgrading a web app from ASP.NET 3 Preview 1 to the RTM and I am confused by the updated approach to dependency injection. I am using StructureMap for this but that's not really relevant to my question. Previously all I needed to do was as follows:

x.For<IControllerFactory>().Use<DefaultControllerFactory>();
x.For<IServiceLocator>().Use(MvcServiceLocator.Current);

现在好像我需要提供IControllerActivator,IViewPageActivator和ModelMetadataProvider的实现,否则我从StructureMap得到一个错误,因为MVC尝试使用的依赖解析器来找到它们。从看MVC源那里似乎没有公开默认实现。我失去了在设置这些点东西?当然,这些应该按照约定进行配置?

Now it seems like I need to provide implementations of IControllerActivator, IViewPageActivator and ModelMetadataProvider because otherwise I get an error from StructureMap because MVC tries to locate them using the dependency resolver. From a look at the MVC source there do not seem to be public default implementations. Am I missing something in setting these up? Surely these should be configured by convention?

的例子有什么需要配置,以及如何与StructureMap将AP preciated。仅供参考我目前使用下面的丑陋杂牌迫使MVC使用其内部默认值:

Examples of what needs configuring and how with StructureMap would be appreciated. For reference I am currently using the following ugly kludge which forces MVC to use its internal defaults:

x.For<IControllerFactory>().Use<DefaultControllerFactory>();
x.For<IDependencyResolver>().Use(() => DependencyResolver.Current);                
x.For<IControllerActivator>().Use(() => null);
x.For<IViewPageActivator>().Use(() => null);
x.For<ModelMetadataProvider>().Use(ModelMetadataProviders.Current);

编辑:只是要清楚,我有一个工作StructureMap实现依赖解析器的 - 问题是,为什么MVC抱怨所有这些接口不被在容器中配置

Just to be clear I have a working StructureMap implementation of the Dependency Resolver - the issue is why MVC is complaining about all these interfaces not being configured in the container.

推荐答案

我已经想通了这一点要归功于链接@迈克尔卡曼贴在他的回答评论。我不知道礼仪的人,此间是否,值得接受他的实际的答案,因为它是不完全正确(我已经给他投票+1),但我想我会发布我自己的答案来解释到底是什么问题是,

I've figured this out thanks to the link @Michael Carman posted in a comment on his answer. I'm not sure of the etiquette here as to whether that warrants accepting his actual answer as it wasn't quite right (I've given him +1 vote) but I thought I'd post my own answer to explain exactly what the issue was.

问题是记在我执行的IDependencyResolver和我的容器配置的组合。本来我有:

The problem was down to a combination of my implementation of IDependencyResolver and my container configuration. Originally I had:

public class StructureMapDependencyResolver : IDependencyResolver
{
    public object GetService(Type serviceType)
    {
        return ObjectFactory.GetInstance(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        foreach (object obj in ObjectFactory.GetAllInstances(serviceType))
        {
            yield return obj;
        }
    }
}

但我现在已经改成此基础上的史蒂夫·史密斯的博客帖子链接到<一个href=\"http://$c$cbetter.com/jeremymiller/2011/01/23/if-you-are-using-structuremap-with-mvc3-please-read-this/\"相对=nofollow>杰里米米勒的博客文章:

public class StructureMapDependencyResolver : IDependencyResolver
{
    public object GetService(Type serviceType)
    {
        if (serviceType.IsAbstract || serviceType.IsInterface)
        {
            return ObjectFactory.TryGetInstance(serviceType);
        }
        else
        {
            return ObjectFactory.GetInstance(serviceType);
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        foreach (object obj in ObjectFactory.GetAllInstances(serviceType))
        {
            yield return obj;
        }
    }
}

在自己这仍然没有解决问题,直到我删除此配置前pression:

on its own this still doesn't resolve the issue until I remove this configuration expression:

x.For<IControllerFactory>().Use<DefaultControllerFactory>();

根据文档TryGetInstance只返回用容器注册类型和,如果不存在将返回null。我presume的MVC 3 code依赖于这种行为,表明它应该在我原来的情况下,我与我的容器注册这些默认设置使用其默认值,因此。棘手的一个!

According to the documentation TryGetInstance only returns types registered with the container and will return null if none exist. I presume the MVC 3 code relies on this behaviour to indicate that it should use its defaults, hence in my original case I had to register these defaults with my container. Tricky one!

这篇关于如何配置依赖注入的ASP.NET MVC 3 RTM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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