1

看来情况就是这样。如果我将泛型标记为封闭类型,则说明我打算使用类型而不是类型的实例。当 Unity 尝试解析下面的 ExternalLinkingRepository 类中的关闭类型时,我收到一个错误。该错误指出 Unity 无法协调构造函数,因为具有相同数量的参数 (2) 的参数超过 1 个。我已经查看了构造函数注入器(并且确实需要一个用于连接字符串,但这不是问题)关于 Unity 为什么尝试实例化关闭类型的任何想法?

这是我的容器和代码的注册片段。我错过了什么或误解了什么:

    // Bootstrapping location...
    .RegisterType<ICampaignRepository, ExternalLinkingRepository>()

    // Concrete Repository -- CampaignDataContext is the cause of issue
    public class ExternalLinkingRepository : RepositoryBase<CampaignDataContext>, ICampaignRepository
    { 
      // blah
    }

    //Base class
     public abstract class RepositoryBase<T> where T : DataContext, IDisposable
     {

       protected T _dataContext;

       public RepositoryBase(String connectionString)
       {

        if (_dataContext == null)
        {
            _dataContext = (T)new DataContext(connectionString);
        }

       }

      public void Dispose()
      {
         if (_dataContext != null)
         {
            GC.SuppressFinalize(_dataContext);
            _dataContext.Connection.Close();
         }
      }

}

编辑 anyaysis 的更详细的代码列表和错误消息 *容器配置*

      IUnityContainer container = new UnityContainer();

        // *****NB**** The code below throws an exception - this is a know bug with Unity
        // If you have the exception manager set to break on "thrown" exceptions (as opposed to just userhandled exceptions) you will be stopped here. 
        // However, the exception IS handled  within the Unity framework and execution of the application can continue safely. The next release of Unity will fix this. 
        // Remove this message when vNext Unity is used. 

        container.RegisterType<IBusinessManager, KnowledgeKubeBusinessManager>()
        .RegisterType<IDataManager, KnowledgeKubeDataManager>()
            // In this instance, specific concrete types are registered for the interfaces specified in the constructor of the UserManagerFactory
        .RegisterType<IUserManagerFactory, UserManagerFactory>(new InjectionConstructor(new ResolvedParameter(typeof(FormsAuthenticatedUserManager)), new ResolvedParameter(typeof(WindowsAuthenticatedUserManager))))
        .RegisterType<IApplicationConfigurationSettings, ApplicastionConfigurationSettings>()
        .RegisterType<IKnowledgeKubeSessionProvider, KnowledgeKubeManagerSessionProvider>()
        .RegisterType<IQuestionnaireQueryArgs, AnsweredKnowledgeQuestionnaireQueryArgs>()
        .RegisterType<ICampaignBusinessManager, CampaignBusinessManager>()
        .RegisterType<ICampaignRepository, ExternalLinkingRepository>(new InjectionConstructor(ConnectionString))


            // Add Interception on KnowledgeKubeDataManager using the  VirtualMethodInterceptor (Much Faster than the TransparentProxyInterceptor)  
        .AddNewExtension<Interception>().Configure<Interception>().SetDefaultInterceptorFor<KnowledgeKubeDataManager>(new VirtualMethodInterceptor())
        .Container.AddNewExtension<Interception>().Configure<Interception>().SetDefaultInterceptorFor<WindowsAuthenticatedUserManager>(new VirtualMethodInterceptor());

ExternalLinkingRepository 配置

/// <summary>
/// TODO: Some refactoring going on here from Campaign to external data linking. 
/// </summary>
public class ExternalLinkingRepository : RepositoryBase<CampaignDataContext>, ICampaignRepository
{

    public ExternalLinkingRepository(String connectionString) : base(connectionString) { }


      public void GetKnowledgeAreaIDs(String externalURLID, String username, out Guid userID, out Int32 knowledgeGroupID, out Int32 knowledgeQuestionnaireID)
    {
        knowledgeGroupID = 0;
        knowledgeQuestionnaireID = 0;
        userID = Guid.Empty; 

        try
        {
            int productIdx = 0; 
            foreach (var result in _dataContext.usp_GetKnowledgeAreaIDSByExternalURLID(externalURLID,username))
            {
            // blah

错误信息

“/WhiteBox”应用程序中的服务器错误。CampaignDataContext 类型有多个长度为 2 的构造函数。无法消除歧义。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:类型 CampaignDataContext 有多个长度为 2 的构造函数。无法消除歧义。

源错误:

第 115 行:_container = 容器;第 116 行:
第 117 行:container.BuildUp(this as T); 第 118 行:
第 119 行:}

源文件:C:\Development\Acropolis\Development\KnowledgeKube_1.0.0\Acropolis Suite\WhiteBox\WhiteBox\Web\WhiteBoxBasePage.cs 行:117

堆栈跟踪:

[InvalidOperationException:CampaignDataContext 类型有多个 >length 2 的构造函数。无法消除歧义。] Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.FindLongestConstructor(>Type typeToConstruct) 在 e:\Builds\Unity\UnityTemp\Compile\Unity\Unity \Src\ObjectBuilder>\Strategies\BuildPlan\Creation\ConstructorSelectorPolicyBase.cs:113

4

1 回答 1

1

Unity 在设置阶段不会创建对象实例。但是,当您注册一个映射时,Unity 会尝试识别创建实例时要使用的构造函数。默认情况下,Unity 将选择接受最多参数的构造函数(最贪婪的构造函数)。如果有多个构造函数采用最大数量的参数(我不确定,但从您的描述中听起来好像有两个 ctor 采用 1 个参数?)Unity 无法决定使用哪个构造函数,因此会引发异常。要解决此问题,您可以删除其中一个构造函数或明确告诉 Unity 要使用哪个构造函数

container.RegisterType<ICampaignRepository, ExternalLinkingRepository>(
    new InjectionConstructor("myConnectionString"));

或者,如果您有一个 ctor 接受您希望由 Unity 解析的参数,您可以通过它们的类型指定它们

container.RegisterType<ICampaignRepository, ExternalLinkingRepository>(
    new InjectionConstructor(typeof(IMyTypeThatUnityShouldResolve));
于 2012-06-19T18:03:02.180 回答