添加了Unity DI框架,现在得到错误"类型UmbracoAuthorizeAttribute有多个长度为1的构造函数。无法消除歧义。"

时间:2018-05-29 20:35:27

标签: c# umbraco unity-container

我将Unity DI框架作为nuget包添加到我的Umbraco解决方案中。我的统一配置文件如下所示:

public static class UnityConfig
{
    #region Unity Container
    private static Lazy<IUnityContainer> container =
      new Lazy<IUnityContainer>(() =>
      {
          var container = new UnityContainer();
          RegisterTypes(container);
          return container;
      });

    public static IUnityContainer Container => container.Value;
    #endregion

    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterType<IHomeViewModelFactory, HomeViewModelFactory>(new PerResolveLifetimeManager());
    }
}

我可以启动该网站,但是一旦我尝试访问Umbraco后台,我就得到了:

UmbracoAuthorizeAttribute类型有多个长度为1的构造函数。无法消除歧义。

更具体地说,日志文件说:

ERROR Umbraco.Core.UmbracoApplicationBase - An unhandled exception occurred
Unity.Exceptions.ResolutionFailedException: Resolution of the dependency failed, type = 'Umbraco.Web.Mvc.UmbracoAuthorizeAttribute', name = '(none)'.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type UmbracoAuthorizeAttribute has multiple constructors of length 1. Unable to disambiguate.
-----------------------------------------------
At the time of the exception, the container was: 
  Resolving Umbraco.Web.Mvc.UmbracoAuthorizeAttribute,(none)
 ---> System.InvalidOperationException: The type UmbracoAuthorizeAttribute has multiple constructors of length 1. Unable to disambiguate.
   at Unity.ObjectBuilder.BuildPlan.Selection.ConstructorSelectorPolicyBase`1.FindLongestConstructor(Type typeToConstruct)
   at Unity.ObjectBuilder.BuildPlan.Selection.ConstructorSelectorPolicyBase`1.SelectConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination)
   at Unity.ObjectBuilder.BuildPlan.DynamicMethod.Creation.DynamicMethodConstructorStrategy.CreateInstanceBuildupExpression(DynamicBuildPlanGenerationContext buildContext, IBuilderContext context)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

尝试将其添加到统一配置

container.RegisterType<Name of Controller>(new InjectionConstructor());

答案 1 :(得分:1)

UnityConfig.cs中,addind以下语句将解决此问题:

container.RegisterType<UmbracoAuthorizeAttribute>(new InjectionConstructor());