1

どうやらこういうことらしい。ジェネリックをクローズ型としてマークしている場合、型のインスタンスではなく、型を使用する意図を述べています。Unity が以下の ExternalLinkingRepository クラスの終了タイプを解決しようとしているため、エラーが発生しています。このエラーは、同じ数のパラメーター (2) を持つコンストラクターが複数あるため、Unity がコンストラクターを調整できないことを示しています。私はコンストラクタインジェクタを見てきました(実際には接続文字列用に必要ですが、これは問題ではありません)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 のより詳細なコード リストとエラー メッセージ * Container Config *

      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: The type CampaignDataContext has multiple constructors > length 2. Unable to disambiguate.] Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.FindLongestConstructor(>Type typeToConstruct) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity \Src\ObjectBuilder>\Strategies\BuildPlan\Creation\ConstructorSelectorPolicyBase.cs:113

4

1 に答える 1

1

Unity は、セットアップ フェーズ中にオブジェクトのインスタンスを作成しません。ただし、マッピングを登録すると、Unity はインスタンスの作成時に使用するコンストラクターを識別しようとします。デフォルトでは、Unity は最も多くのパラメーターを受け取るコンストラクター (最も貪欲なコンストラクター) を選択します。最大数のパラメーターを取るコンストラクターが複数ある場合 (よくわかりませんが、あなたの説明からすると、1 つのパラメーターを取る 2 つの ctor があるように聞こえますか?) Unity はどのコンストラクターを使用するかを決定できず、例外をスローします。この問題を解決するには、コンストラクターの 1 つを削除するか、使用するコンストラクターを Unity に明示的に指示します。

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

または、Unity で解決したい引数を取る ctor がある場合は、型で指定できます

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