Skip to content
Thomas Stegemann edited this page May 20, 2014 · 19 revisions

Loading XML modules

In order to use XML configuration you need to create one or more XML files that contain an XML configuration module and publish them with your solution. Once you have created these XML modules you need to tell the kernel to load them using the Load method:

this.Kernel.Load("myXmlConfigurationModule.xml");

Basic XML Configuration Syntax

The xml file must contain an xml configuration module like shown this the following example:

<module name="myXmlConfigurationModule">
    <bind service="MyNamespace.IMyService, MyAssembly"
          to="MyNamespace.MyServiceImplementation, MyAssembly" />
    <bind service="MyNamespace.IMyOtherService, MyAssembly"
          to="MyNamespace.MyOtherServiceImplementation, MyAssembly" />
</module>

module Element

The xml configuration must contain the root element module. This is used to define the unique name of the module using its name attribute. The module element contains one or more bind elements to configure the bindings.

bind Element

Each bind element defines one Ninject binding. It is comparable to a call to kernel.Bind<IService>().To<Service>().

service attribute

The service attribute defines the servce type of the binding. It is equivalent to the kernel.Bind() method.

to attribute

The to attribute defines the implementation type of the binding. It is equivalent to the .To<Service>() method.

toProvider attribute

Alternatively to the "to" attibute you can use the "toProvider" attribute to define a Ninject provider instead of an implementation type. It is equivalent to the .ToProvider<ServiceProvider>() method.

name attribute (optional)

In case you want to configure a named binding you can add an optional "name" attribute to the binding to define the name of the binding.

scope attribute (optional)

A binding can contain an optional scope attribute that defines the scope. It defaults to transient if none is declared. The following values are allowed:

  • transient: is equivalent to InTransientScope()
  • singleton: is equivalent to InSingletonScope()
  • thread: is equivalent to InThreadScope()
  • request: is equivalent to InRequestScope() this option requires that you are using the Ninject.Web.Common.Xml extension

Type definitions

All the types configured by the bindings must be defined as assembly qualified names: http://msdn.microsoft.com/de-de/library/system.type.assemblyqualifiedname.aspx

Continue reading: Metadata