The ServiceLocator in Catel can be set up from configuration file.
Table of Contents
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:
...
Note |
---|
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.
...
Code Block | ||
---|---|---|
| ||
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:
...