0

我想讓Unity成爲工廠模式中的工廠,因此根據命名實例返回接口的不同實現。我有以下代碼Unity通過名稱解析

public static class工廠 {0}私有靜態只讀IUnityContainer容器;

static Factory() 
    { 
     container = new UnityContainer(); 
     container 
      .AddNewExtension<EnterpriseLibraryCoreExtension>() 
      .AddNewExtension<Interception>() 
      .RegisterType<IInterceptionBehavior, LoggingBehaviour>() 

      //register the implemantation1 
      .RegisterType<IMessageFactory, implemantation1>("implemantation1") 
      .RegisterType<IMessageFactory, implemantation1>(new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"])) 
      .RegisterType<IMessageFactory, implemantation1>(new InjectionProperty("ServerPort", int.Parse(ConfigurationManager.AppSettings["Port"]))) 
      .RegisterType<IMessageFactory, implemantation1>(
       new Interceptor<InterfaceInterceptor>(), 
       new InterceptionBehavior(container.Resolve<IInterceptionBehavior>())) 

      //register the implemantation2 
      .RegisterType<IMessageFactory, implemantation2>("implemantation2") 
      .RegisterType<IMessageFactory, implemantation2>(new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"])) 
      .RegisterType<IMessageFactory, implemantation2>(new InjectionProperty("Port", int.Parse(ConfigurationManager.AppSettings["Port"]))) 
      .RegisterType<IMessageFactory, implemantation2>(
       new Interceptor<InterfaceInterceptor>(), 
       new InterceptionBehavior(container.Resolve<IInterceptionBehavior>())) 
      ; 
    } 

    /// Instantiates a data provider class 
    public static T CreateFactory<T>(string name) 
    { 
     return container.Resolve<T>(name); 
    } 
} 

當我嘗試通過調用CreateFactory法「implemantation2」或「implemantation1」來解決容器我得到以下錯誤: 出現InvalidOperationException - 不能構建String類型。您必須配置容器以提供此值。

我不知道我在做什麼錯,任何幫助將不勝感激。

喬希

回答

0

答案是.....

static Factory() 
    { 
     container = new UnityContainer(); 
     container 
      .AddNewExtension<EnterpriseLibraryCoreExtension>() 
      .AddNewExtension<Interception>() 
      .RegisterType<IInterceptionBehavior, LoggingBehaviour>() 

      //register the implemantation1 
      .RegisterType<IMessageFactory, implemantation1>("implemantation1", new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"])) 
      .RegisterType<IMessageFactory, implemantation1>("implemantation1", new InjectionProperty("Port", int.Parse(ConfigurationManager.AppSettings["Port"]))) 
      .RegisterType<IMessageFactory, implemantation1>("implemantation1", 
       new Interceptor<InterfaceInterceptor>(), 
       new InterceptionBehavior(container.Resolve<IInterceptionBehavior>())) 

      //register the implemantation2 
      .RegisterType<IMessageFactory, implemantation2>("implemantation2", new InjectionProperty("WSIPServer", ConfigurationManager.AppSettings["WSIPServer"])) 
      .RegisterType<IMessageFactory, implemantation2>("implemantation2", new InjectionProperty("WSIPServerPort", int.Parse(ConfigurationManager.AppSettings["WSIPServerPort"]))) 
      .RegisterType<IMessageFactory, implemantation2>("implemantation2", 
       new Interceptor<InterfaceInterceptor>(), 
       new InterceptionBehavior(container.Resolve<IInterceptionBehavior>())) 
      ; 
    } 
0

你必須把所有的注射設置在寄存器中的呼叫:

.RegisterType<IMessageFactory, implemantation1>(
      "implemantation1", 
      new InjectionProperty("Server", ConfigurationManager.AppSettings["Server"]), 
      new InjectionProperty("ServerPort", int.Parse(ConfigurationManager.AppSettings["Port"]), 
      new Interceptor<InterfaceInterceptor>(), 
      new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))