2

DefaultControllerFactoryでDIが設定されたASP.NETMVC3を使用して、Unity2で登録されたタイプの名前付き登録を解決する際に問題が発生します。

1つのアセンブリで、登録タイプと名前付き登録を使用してUnityコンテナーを定義しました

public class VisUnityContainer : UnityContainer
{
    public IUnityContainer RegisterVisComponents()
    {
         // register types
         this               
             .RegisterType<ICompanyService, CompanyService>()
             .RegisterType<ICompanyService, TestCompanyService>( "2" );
    }
}

そして、MVCプロジェクトでは、DefaultControllerFactoryを継承し、型を解決してVisUnityContainerを渡します。

public class UnityControllerFactory : DefaultControllerFactory
{
    private readonly IUnityContainer unityContainer;

    public UnityControllerFactory( IUnityContainer unityContainer )
    {
        // set contracts
        if( unityContainer == null )
            throw new ArgumentNullException( null, "Unity container is not initialized." );

        // set associations
        this.unityContainer = unityContainer;
    }

    protected override IController GetControllerInstance( RequestContext requestContext, Type controllerType )
    {
        // set contracts
        if( controllerType == null )
            throw new HttpException( 404, String.Format(  "The controller for path '{0}' could not be found or it does not implement IController.",
                     requestContext.HttpContext.Request.Path ) );
        if( !typeof( IController ).IsAssignableFrom( controllerType ) )
            throw new ArgumentException( String.Format( "Type requested is not a controller: {0}", controllerType.Name ) );

        // action result
        IController controller;

        // company law
        string companyLaw = String.Empty;

        // set user properties
        if( Thread.CurrentPrincipal != null &&
            Thread.CurrentPrincipal.Identity.Name != null &&
            !String.IsNullOrWhiteSpace( Thread.CurrentPrincipal.Identity.Name ) )
        {
            // set culture for law of companies region
            CultureInfo cultureInfo = new CultureInfo( Profile.GetCurrent().CompanyState );

            // set language
            CultureInfo uiCultureInfo = new CultureInfo( Profile.GetCurrent().UserLanguage );

            // set dates etc.
            Thread.CurrentThread.CurrentCulture = cultureInfo;

            // get proper resource file
            Thread.CurrentThread.CurrentUICulture = uiCultureInfo;

            // set company law
            companyLaw = Profile.GetCurrent().CompanyLaw;
        }

        try
        {
            // resolve container
            controller = this.unityContainer.Resolve( controllerType, companyLaw ) as IController;
        }
        catch( Exception )
        {
            // throw exception
            throw new InvalidOperationException( String.Format( "Error resolving controller {0}", controllerType.Name ) );
        }

        // action end
        return controller;

    }
}

問題はラインにあります

controller = this.unityContainer.Resolve( controllerType, companyLaw ) as IController;

companyLawは2に等しいですが、名前付き登録TestCompanyServiceを解決しませんが、常にCompanyServiceを解決します。名前付き登録を使用してCompanyServiceも設定すると、タイプを解決できないというエラーがスローされます。

また、手動で次のようなタイプを解決しようとすると

var test = this.unityContainer.Resolve<ICompanyService>( companyLaw );

正しいタイプを返します。

誰かが何が悪いのか考えていますか?

4

1 に答える 1

1
Container.Resolve(type,string)

Container.Resolve<T>(string)

異なる方法ではありません。

Resolve(string) は、実際には内部で Container.Resolve(type,string) を呼び出す拡張メソッドです。

コードを見ると、「controllerType」はクラスですか、インターフェースですか。"controllerType" がインターフェイス (ICompanyService) ではなくクラス (CompanyService) である場合、常に CompanyService に解決されます。また、しようとしたときにエラーが発生する理由も説明します

Resolve(controllerType,"2")

ここで実際に言っていることは次のとおりです。

container.Resolve(typeof(CompanyService),"2");

存在しません。

これは、次のように呼び出したときに機能する理由も説明します。

container.Resolve<ICompanyService>("2");

ここで正しいインターフェースを設定しているため

だから、短い答え:

Unity へのインターフェイスではなく、具体的なクラス タイプを渡していると思います。かなり簡単にチェックできるはずです。

于 2012-06-22T10:20:57.533 に答える