团结来解析名称(Unity to Resolve By Name)

2019-10-17 04:57发布

我试图让团结是我在一个工厂模式的工厂,因此返回取决于命名实例接口的不同实现。 我有以下代码

公共静态类工厂{私人静态只读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类型。 您必须配置来提供此值的容器。

我不知道我做错了任何帮助,将不胜感激。

玩笑

Answer 1:

答案是.....

    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>()))
            ;
    }


Answer 2:

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

.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>()))


文章来源: Unity to Resolve By Name