Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Web.Mvc类型的Unity ResolutionFailedException_C#_Asp.net_.net_Asp.net Mvc 4_Unity Container - Fatal编程技术网

C# System.Web.Mvc类型的Unity ResolutionFailedException

C# System.Web.Mvc类型的Unity ResolutionFailedException,c#,asp.net,.net,asp.net-mvc-4,unity-container,C#,Asp.net,.net,Asp.net Mvc 4,Unity Container,我的WebApp是WebForms页面和MVC4页面的组合 使用Unity,我得到以下异常 异常类型: Microsoft.Practices.Unity.ResolutionFailedException, Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Resolution of the dependency failed, type = "Syst

我的WebApp是WebForms页面和MVC4页面的组合

使用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)
The type ModelMetadataProvider does not have an accessible constructor.
消息

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)
The type ModelMetadataProvider does not have an accessible constructor.
具有以下
内部异常

消息

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)
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
在应用程序启动中,我将通过以下方式建立国际奥委会:

DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfigurator.GetContainer()));
…其中
GetContainer
返回所有注册的类型和
unitydependencysolver
is-a
idependencysolver

你知道是什么导致了这个问题吗

谢谢,

--Ed

我不确定UnityConfigurator.GetContainer()返回的容器实例如何准确运行,但我假设它会在需要时尝试单独解析
ModelMetadataProvider
。此
modelmataprovider
具有一些容器不知道的依赖项,只有初始MVC解析器才能解析这些依赖项。“初始MVC解析器”是指调用
DependencyResolver.SetResolver(…)
之前的
DependencyResolver.Current
的结果。 因此,您需要在自己的容器上实现代理,并将初始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\u Start()
中,使用此代理的实例覆盖MVC解析器,如下所示:

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

你们有旅行社吗?您是否为MVC设置了依赖项解析器?