2010-03-31 61 views
3

我使用温莎城堡进行了IoC,并有在web.config/app.config举行的配置,使用下面的工厂:温莎城堡和自己的配置文件

public static TYPE Factory(string component) 
    { 
     var windsorContainer = new WindsorContainer(new XmlInterpreter()); 
     var service = windsorContainer.Resolve<TYPE>(component); 

     if (service == null) 
      throw new ArgumentNullException(string.Format("Unable to find container {0}", component)); 

     return service; 
    } 

和我web.config看起来像这样:

<configuration> 
    <configSections> 
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/> 
    </configSections> 
    <castle> 
    <components> 
     <component id="Data" service="Data.IData, Data" type="Data.DataService, Data"/> 
    </components> 
    </castle> 
    <appSettings>....... 

哪些工作正常,但我想将温莎城堡的配置放在名为castle.config的文件中。我该怎么做呢?

+0

注意,我们在每次创建一个新的容器你需要解析一个组件。这不仅**效率很低,而且还会以这种方式失去生活方式管理等功能。相反,请为每个应用程序创建一个容器。 – 2010-03-31 12:27:45

+2

请告诉我你只是在你的应用程序的每个生命周期中调用这个方法'Factory' ... – 2010-03-31 15:39:58

+0

它只是作为WCF服务的构造函数的一部分被调用,是吗? – Coppermill 2010-04-01 08:03:59

回答

4

WindsorContainer将接受配置文件作为施工参数的名称:

公共WindsorContainer(字符串XMLFILE)

摘要:初始化的 的Castle.Windsor.WindsorContainer 类的新实例使用xml文件来配置 它。相当于使用新 WindsorContainer(新 XmlInterpreter(XMLFILE))

你castle.config文件,那么应该是这样的:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <components> ...