Share via


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.

A service is an object that provides functionality in a loosely coupled way to other components. These components can be in the same module or in other modules. The Web Client Software Factory includes a set of basic services that you can use in your applications. You can also develop your own services to provide infrastructure capabilities that are specific to your applications.

The WebClientApplication class loads default implementations of several services and places them into the root container. These services are available to all the modules in the application. Figure 1 illustrates the collection of default services.

Note

Although it is possible for a module to navigate the composition container hierarchy to access services in another module, it is not recommended. A module should retrieve shared services from the root composition container. This means that all shared services should be located in the root container.

Ff709838.5596b894-81a5-4fe3-ac03-1b08b3052c70(en-us,PandP.10).png

Figure 12

Services available to all modules

The services include the following:

  • ModuleConfigurationLocatorService. This service locates the configuration information for a module.
  • VirtualPathUtilityService. This service contains useful methods for managing virtual paths and URLs.
  • AuthorizationRulesService. This service registers and returns registered rules for an URL.
  • SessionStateLocatorService. This service encapsulates the access to System.Web.HttpContext.Current.Session.
  • HttpContextLocatorService. This service encapsulates the access to System.Web.HttpContext.Current.
  • ModuleLoaderService. This service loads module assemblies.
  • WebModuleEnumerator. This service extracts module information from all Web.config files that are located the site folder hierarchy.
  • ModuleContainerLocatorService. This service locates the composition container for a module.
  • ServiceLoaderService. This service loads services into a composition container.
  • WebConfigModuleInfoStore. This service reads configuration files to retrieve modules metadata.

Registering a Service

You can either programmatically register a service or register it through configuration. To programmatically register a service, call the Add method or the AddNew method of the Services collection of the composition container within which you want to use the service. This can be either the root composition container or a module composition container. To use an existing service instance, use the Add method. To create a service, use the AddNew method. The following code example shows how to create a new service.

moduleCompositionContainer.Services.AddNew<AccountServices, IAccountServices>();

Locating Services

Services are stored in the Services collection of the composition containers. Each composition container exposes a Services property that you can use to register and locate services. A Services collection can have only one instance of a service type, but you can add multiple instances of the same type to the Services collections of different composition containers.

Figure 2 illustrates an example of a composition container hierarchy for an application that has two business modules. The root container contains two services. Every module has access to the root container and to the services it contains.

A container can contain only a single instance of a service type, but instances of the same service type can be placed in multiple containers. When a module attempts to locate a service, the Composite Web Application Block first examines the collection of services in the container for the module. If it does not find the service there, the application block looks in the collection of services in the root container. This means that a module can override the global services available in the root container and leave the global service unchanged.

Ff709838.4b412f14-02bc-442b-b913-422e00a54890(en-us,PandP.10).png

Figure 13

Composition container hierarchy

The Services collection implements the IServiceCollection interface. This interface defines two methods you can use to register services:

  • Add. You use this method to register an existing instance of a service. The ObjectBuilder strategies will be executed on the instance and it will be added to the collection.

    services.Add(myService);
    
  • AddNew. You use this method to have ObjectBuilder create an instance of the service and register it in the services collection.

    services.AddNew(typeof(MyService));
    
    // using generics:
    services.AddNew<MyService>();
    

Both the Add and AddNew methods provide overloaded versions that allow you to specify the type that will be used to register the service. This is the type developers use to obtain a reference to the service, as shown in the following code. Any services that you add by using the default AddNew<TService> method are registered as typeof(TService).

// AddNew method
services.AddNew(typeof(MyService), typeof(IMyService));
// AddNew method using generics:
services.AddNew<MyService, IMyService>();

// Add method
services.Add(typeof(IMyService), myService);
// Add method using generics:
services.Add<IMyService>(myService);

The following code is typical of code that you write in a module. In this example, the module registers both a global service and a module service.

public override void Load(CompositionContainer moduleContainer)
{
  base.Load(moduleContainer);

  AddGlobalServices(moduleContainer.Parent.Services);
  AddModuleServices(moduleContainer.Services);
   RegisterSiteMapInformation(moduleContainer.Services.Get<ISiteMapBuilderService>(true));
}

protected virtual void AddGlobalServices(IServiceCollection globalServices)
{
  globalServices.AddNew<FindCustomerService, IFindCustomerService>();
}

protected virtual void AddModuleServices(IServiceCollection moduleServices)
{
  moduleServices.AddNew<OrdersService, IOrdersService>();
}

It is also possible to register a service through configuration. This means you can change the registration without modifying code. To register a service through configuration, you add a service configuration element to a Web.config file (either the main Web site Web.config file or a module Web.config file). In the following XML, the OrdersRepository.Services.CustomerService service is registered as a global service. (To register a service as a module service, change the scope attribute to Module.)

<compositeWeb>
  <modules>
    <module name="Customers" assemblyName="Customers" virtualPath="~/Customers">
      . . . 
      <services>
        <service registerAs="OrdersRepository.Interfaces.Services.ICustomerService, OrdersRepository.Interfaces" type="OrdersRepository.Services.CustomerService, OrdersRepository.Services" scope="Global" />
      </services>
    </module>
  </modules>
      . . .
</compositeWeb>

Note

Note: For detailed information about how to register a service, see How to: Register and Use Services.