首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在统一和泛型的情况下,在容器旋转过程中是否注入关闭类型的实例,如果是的话,为什么?

在统一和泛型的情况下,在容器旋转过程中是否注入关闭类型的实例,如果是的话,为什么?
EN

Stack Overflow用户
提问于 2012-06-19 16:30:23
回答 1查看 1.4K关注 0票数 1

看来是这样的。如果我将泛型标记为封闭类型,则说明我打算使用type而不是Type的实例。我收到一个错误,因为Unity正试图解析下面ExternalLinkingRepository类中的关闭类型。错误声明,由于有多于1的参数(2),联合不能协调构造函数。我已经看过构造函数注入器(确实需要一个用于连接字符串,但这不是问题),有什么想法,为什么团结尝试实例化关闭类型?

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

代码语言:javascript
复制
    // 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 *容器Config*编辑更详细的代码列表和错误消息

代码语言:javascript
复制
      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 config

代码语言:javascript
复制
/// <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(作为T);第118行:

第119行:}

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

堆栈跟踪:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-19 18:03:02

在安装阶段,统一不会创建对象的实例。但是,当您注册一个映射时,联合尝试标识在创建实例时要使用的构造函数。默认情况下,联合将选择参数最多的构造函数(最贪婪的构造函数)。如果有多个构造函数接受参数的最大数量(我不确定,但从您的描述来看,似乎有两个ctors接受一个参数?)统一不能决定使用哪个构造函数,从而引发异常。要解决这个问题,可以删除其中一个构造函数,也可以明确地告诉团结使用哪个构造函数

代码语言:javascript
复制
container.RegisterType<ICampaignRepository, ExternalLinkingRepository>(
    new InjectionConstructor("myConnectionString"));

或者,如果您的ctor接受了您希望由Unity解析的参数,则可以根据它们的类型指定它们

代码语言:javascript
复制
container.RegisterType<ICampaignRepository, ExternalLinkingRepository>(
    new InjectionConstructor(typeof(IMyTypeThatUnityShouldResolve));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11105331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档