Adding Services

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
To create client business applications using current Microsoft technologies, see patterns & practices' Prism.

After creating the shell, you may need to add additional services that your application requires. By default, the FormShellApplication class automatically adds the following services types when you create the host:

  • SimpleWorkItemActivationService implementation of IWorkItemActivationService
  • WindowsPrincipalAuthenticationService implementation of IAuthenticationService
  • ModuleLoaderService implementation of IModuleLoaderService
  • DataProtectionCryptographyService implementation of ICryptographyService
  • TraceSourceCatalogService implementation of ITraceSourceCatalogService
  • CommandAdapterMapService implementation of ICommandAdapterMapService
  • WorkItemExtensionService implementation of IWorkItemExtensionService
  • WorkItemTypeCatalogService implementation of IWorkItemTypeCatalogService
  • FileCatalogModuleEnumerator implementation of IModuleEnumerator
  • ControlActivationService implementation of IControlActivationService

However, if you want to use other services such as the FileStatePersistenceService for saving WorkItem state (which does not load by default) you must first add them to the root WorkItem. You can do this by specifying the service in the shell application configuration, programmatically in your code, or declaratively by using an attribute.

To add a service by specifying it in the shell application configuration file

  1. Add a section named CompositeUI to the application configuration.
  2. In this section, list the services that should be loaded as shown in the following code.
<?xml version="1.0" encoding="utf-8" ?>
    <section name="CompositeUI"
      allowExeDefinition="MachineToLocalUser" />
      <!-- Other services -->
      <add serviceType="MyApp.Services.IMyService, MyApp"
           instanceType="MyApp.Services.MyService, MyApp"/>

To add a service programmatically

  1. Call the Add or AddNew method of the Services collection of the WorkItem within which you want to use the service. This can be the root WorkItem or another WorkItem that you create. To use an existing service instance you have already created, use the Add method. You could use the following code in an override of the Start method in your custom application class:
  1. To create a new instance of a service and add it to the WorkItem, use the AddNew method.

An exception is thrown if the service you add is already registered in the current WorkItem. However, the same service can exist in parent WorkItems.

Note: The CabApplication class provides a virtual method named AddServices that should be used to add services at startup.

To declare a class to be registered as a service using an attribute

  1. You can place the Service attribute on a public class. The Composite UI Application Block infrastructure creates an instance of this class and registers it as a service during application startup. The constructor parameter tells the Composite UI Application Block that, for location purposes, the service will be registered under the interface key:
public class MyService : IMyService
  1. Services that do not provide different implementations may not implement an interface. In this case, you can register the service without using an interface reference.
public class MyService

To declare a class to be registered as a lazy-loaded service

  1. Use the AddOnDemand property of the Service attribute to specify that the service should load on demand (only when actually requested).
[Service(typeof(IMyService), AddOnDemand=true)]
public class MyService : IMyService

For more information about using services in your applications, see Getting References to Services. For more information about creating your own services, see Creating Custom Services.