Home

Castle Stronghold

Initializing with an external configuration

The strong point about Windsor Container is its support to external configuration. This document will explore the constructors overloads offered by it, and the options on where to keep configuration files.

The formal reference can be found on Windsor Configuration Reference document.

Using a standalone configuration file

You can create a xml file and use it to configure the container. The expected format follows:


<configuration>

    <include .../>
    <include .../>
    
    <properties>
        ...
    </properties>

    <facilities>
        ...
    </facilities>
    
    <components>
        ...
    </components>

</configuration>

You can then create a WindsorContainer instance using the following code:


using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;    

IWindsorContainer container = 
    new WindsorContainer(new XmlInterpreter(new FileResource("filename.xml")));

// or simply

IWindsorContainer container = 
    new WindsorContainer(new XmlInterpreter("filename.xml"));
    

If you use includes on the configuration file, be aware the the URIs will be relative to the location of the configuration file, in the example above, it will be relative to the folder where filename.xml is.

Using the AppDomain configuration file

The AppDomain configuration file is the one that uses the application name plues .config or the web.config for web applications.

To use them you must register a section and then include the configuration in a nome called castle. For example:


<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
    <configSections>
        <section 
            name="castle"
            type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
    </configSections>

    <castle>
    
        <include .../>
        <include .../>
        
        <properties>
            ...
        </properties>

        <facilities>
            ...
        </facilities>
        
        <components>
            ...
        </components>
    
    </castle>

</configuration>

You can then create a WindsorContainer instance using the following code:


using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;    

IWindsorContainer container = 
    new WindsorContainer(new XmlInterpreter(new ConfigResource("filename.xml")));

// or simply

IWindsorContainer container = 
    new WindsorContainer(new XmlInterpreter());
    

If you use includes on the configuration file, be aware the the URIs will be relative to the AppDomain base path.

Google
Search WWW Search castleproject.org