System.Web.Mvc类型的Unity ResolutionFailedException

时间:2014-03-11 17:23:39

标签: c# asp.net .net asp.net-mvc-4 unity-container

我的WebApp是WebForms页面和MVC 4页面的组合。

使用Unity,我收到以下异常。

例外类型

Microsoft.Practices.Unity.ResolutionFailedException, Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

消息

Resolution of the dependency failed, type = "System.Web.Mvc.ModelMetadataProvider", name = "(none)".  Exception occurred while: while resolving.  Exception is: InvalidOperationException - The type ModelMetadataProvider does not have an accessible constructor.  At the time of the exception, the container was:
Resolving System.Web.Mvc.ModelMetadataProvider,(none)

Inner Exception

消息

The type ModelMetadataProvider does not have an accessible constructor.

堆栈跟踪

at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObje ct(IBuilderContext context)  
at BuildUp_System.Web.Mvc.ModelMetadataProvider(IBuilderContext )  
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)  
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)  
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)  
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)  

对于以下类型,我始终获得相同的分辨率异常:

System.Web.Mvc.ITempDataProvide  
System.Web.Mvc.Async.IAsyncActionInvoker  
System.Web.Mvc.IActionInvoker  
System.Web.Mvc.ModelMetadataProvider  
System.Web.Mvc.IViewPageActivator

在Application_Start中,我正在设置IoC:

DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfigurator.GetContainer()));

...其中GetContainer返回所有已注册的类型,而UnityDependencyResolver是-a IDependencyResolver

任何想法是什么导致了这个问题?

谢谢,
--ed

1 个答案:

答案 0 :(得分:1)

我不确定UnityConfigurator.GetContainer()返回的容器实例是如何完全行为的,但我认为它会在需要时单独尝试解析ModelMetadataProvider。这个ModelMetadataProvider有一些依赖关系,你的容器不知道只有初始的MVC解析器才能解析。 “初始MVC解析器”是指DependencyResolver.Current之前调用DependencyResolver.SetResolver(...)的结果。 因此,您需要在自己的容器上实现代理,并将其用作初始MVC解析器的后备。

代理实现方式的快速示例:

public class MvcDependencyResolver:System.Web.Mvc.IDependencyResolver
{
    private readonly IDependencyResolver _resolver;

    private readonly System.Web.Mvc.IDependencyResolver _fallbackResolver;

    public MvcDependencyResolver(IDependencyResolver resolver, System.Web.Mvc.IDependencyResolver fallbackResolver)
    {
        this._resolver = resolver;
        this._fallbackResolver = fallbackResolver;
    }

    public object GetService(Type serviceType)
    {
        if ((this._resolver.IsRegistered(serviceType)) || (serviceType.IsClass))
        {
            try
            {
                return this._resolver.GetService(serviceType);
            }
            catch (ResolutionFailedException)
            {
            }
        }
        return this._fallbackResolver.GetService(serviceType);
    }

而且,在Application_Start()中,您使用此代理的实例覆盖MVC解析器,如下所示:

var bootstrapper = new Bootstrapper();
var resolver = bootstrapper.Run();
DependencyResolver.SetResolver(new MvcDependencyResolver(resolver, DependencyResolver.Current));