How to: Load Modules on Demand


A module can be loaded on demand during the application life cycle instead of being loaded during the application startup. For example, a module can be loaded on demand to delay the initialization of a resource-demanding feature of the application until it is required.

The purpose of this topic is to describe how to load modules on demand.


This topic assumes that you have a solution built with the Composite Application Library that has a module configured to be statically or dynamically loaded. For information about how to do this, see the following topics:


To load a module on demand

  1. Specify that the module you want to load on demand must not be loaded on startup. This step varies, depending on the method used to define the modules in the catalog.

    If you intend to define the modules in a Silverlight or WPF application, see the following topics for information about how to perform this step:

    In a WPF application, you can also populate the module catalog from a configuration file or a directory. If you are using one of these approaches, see the following topic for information about how to perform this step:

  2. Obtain a reference to the module manager service (this service implements the IModuleManager interface). To do this, you can use constructor dependency injection by adding a parameter to the class constructor that receives an instance of the module manager service.
    To use constructor dependency injection, your class must be instantiated using a dependency injection container. The container must be able to resolve the module manager and module loader services. If you are using the default UnityBootstrapper class to start your application, the container is already configured.

    The following code shows a class that receives a module manager service instance as a parameter, and then it stores it in instance variables.

    private readonly IModuleManager moduleManager;
    public DefaultViewB(IModuleManager moduleManager)
        this.moduleManager = moduleManager;
  3. Load the module by invoking the LoadModule method on the module manager service instance, providing the module name as a parameter. The following code shows how to do this.


A module gets loaded on demand.

More Information

For more information related to working with modules, see the following topics:

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.

Home page on MSDN | Community site