Child pages
  • Setting up the ServiceLocator using configuration
Skip to end of metadata
Go to start of metadata

The  ServiceLocator in Catel can be set up from configuration file.


Importing IoC configuration section

The first step to setup the service locator from the configuration file is import the custom section type Catel.IoC.IoCConfigurationSection from Catel.Core. The following example shows how to import this configuration section and make it available for the configuration file as ioc:

<configuration>
  <configSections>
       <sectionGroup name="catel">
           <section name="ioc" type="Catel.IoC.IoCConfigurationSection, Catel.Core" />
       </sectionGroup>
   </configSections>
   ...
</configuration>

In the example above we also create a section group named catel to group all Catel related configuration sections.

Configuring a service locator from the default configuration

It's possible add more than one service locator configuration to the configuration file but you must specify an unique name. If a name of a service locator configuration is not specified then the name default is assigned. By default such configuration supports dependency injection.

<configuration>
   <configSections>
       <sectionGroup name="catel">
           <section name="ioc" type="Catel.IoC.IoCConfigurationSection, Catel.Core" />
       </sectionGroup>
   </configSections>
   <catel>
       <ioc>
           <serviceLocatorConfigurations>
               <serviceLocatorConfiguration [name="default"]>
                   <register interfaceType="Catel.MVVM.Services.IUIVisualizerService" implementationType="Catel.MVVM.Services.UIVisualizerService" />
                   <register interfaceType="Catel.MVVM.Services.IProcessService" implementationType="Catel.MVVM.Services.ProcessService" />
               </serviceLocatorConfiguration>
        </serviceLocatorConfigurations>
       </ioc>
   </catel>
</configuration>

 

To configure a service locator from the default service locator configuration use the following code:

 

var serviceLocator = ServiceLocator.Default;

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var ioc = configuration.GetSection<IoCConfigurationSection>("ioc", "catel");

ioc.DefaultServiceLocatorConfiguration.Configure(serviceLocator);

 

Configuring a service locator from a named configuration

The following configuration file is a full example on how write more than one service locator configuration:

<configuration>
   <configSections>
       <sectionGroup name="catel">
           <section name="ioc" type="Catel.IoC.IoCConfigurationSection, Catel.Core" />
       </sectionGroup>
   </configSections>
   <catel>
       <ioc>
           <serviceLocatorConfigurations>
               <serviceLocatorConfiguration>
                   <register interfaceType="Catel.MVVM.Services.IUIVisualizerService" implementationType="Catel.MVVM.Services.UIVisualizerService" />
                   <register interfaceType="Catel.MVVM.Services.IProcessService" implementationType="Catel.MVVM.Services.ProcessService" />
               </serviceLocatorConfiguration>
               <serviceLocatorConfiguration name="test" supportDependencyInjection="false">
                   <register interfaceType="Catel.MVVM.Services.IUIVisualizerService" implementationType="Catel.MVVM.Services.Test.UIVisualizerService" registrationType="Transient"/>
                   <register interfaceType="Catel.MVVM.Services.IProcessService" implementationType="Catel.MVVM.Services.Test.ProcessService" tag="test"/>
               </serviceLocatorConfiguration>
           </serviceLocatorConfigurations>
       </ioc>
   </catel>
</configuration>

To configure a service locator from a named configuration use the following code:

var serviceLocator = ServiceLocator.Default;

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var ioc = configuration.GetSection<IoCConfigurationSection>("ioc", "catel");

ioc.GetServiceLocatorConfiguration("test").Configure(serviceLocator);

You should also note the options setup if the container in order to support dependency injection and the registration type (a.k.a instantiation style) and the tag for each registration.

  • No labels

2 Comments

  1. Hi Geert,

    Thanks for a very nice set of components first of all.


    I need some of your guidance.

    What I'm trying to achieve is the following:

    I'm working on WPF plugin which is hosted in an application and we don't have control on it's configuration file.

    Plugin has it's own exception handler which shuts it down in case of unhandled error and then restarts the plugin. One of the goals here is to clean up all state, so I'm using an instance of ServiceLocator which I then dispose and create new one.

    I'm struggling to figure out how can I wire this instance up so it can be used to construct views, view models, etc.


    Could you give me a hint on that?


    Thanks,

    Igor