Share via


Registering a Service with the SharePoint Service Locator

Typical Goals

In this scenario, you want to register the implementation for a specific interface with the SharePoint service locator. For example, you want to register a mapping of the IPricingRepository interface to the PricingRepository class that provides an implementation of IPricingRepository. This scenario occurs when a SharePoint feature is deployed that provides a new implementation of a class.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll, and to Microsoft.Practices.ServiceLocation.dll. Use the SharePoint service locator to get a reference to a Microsoft.Practices.SPG.Common.ServiceLocation.IServiceLocatorConfig interface and invoke the RegisterTypeMapping method. Use the interface and the implementation class you want your application class to use at run time as type parameters to the RegisterTypeMapping method.

Registering a Service with the SharePoint Service Locator

The following code shows how to add a service to an application's configuration from within a feature receiver class.

using Microsoft.SharePoint;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SPG.Common.ServiceLocation; 

[CLSCompliant(false)]
[Guid("8b0f085e-72a0-4d9f-ac74-0038dc0f6dd5")]
public class MyFeatureReceiver : SPFeatureReceiver
{ 
    /// ...

    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
       // Get the ServiceLocatorConfig service from the service locator.
       IServiceLocator serviceLocator = SharePointServiceLocator.Current;
       IServiceLocatorConfig typeMappings =  
                                 serviceLocator.GetInstance<IServiceLocatorConfig>();

       typeMappings.RegisterTypeMapping<IPricingRepository, PricingRepository>();
    }   
} 

The FeatureActivated method registers the PricingRepository class as the configured implementation of the IPricingRepository interface.

For more information about using feature receivers, see Working with Features on MSDN.

Usage Notes

The RegisterTypeMapping method has three overloaded versions. You can use these to provide a name for the type mapping and to indicate whether a new instance of the implementation class is provided with every service location request or whether a single shared instance is used for all requests. By default, a new instance is created with every service location request.

Note

It is generally recommended that you do not use the service locator to register types as singletons. Registering a service as a singleton requires the service to be thread safe. Unless you have verified that your service is thread safe, you should not share a single instance of your object across all application threads.

If you want to remove a type mapping, you must either explicitly remove it using code or register a new type mapping to replace it. Type mappings are not automatically removed when a feature is deactivated or uninstalled.

If an interface appears in more than one type mapping, the most recently registered type mapping will be used to return implementation instances.

For an example of how to get a service instance based on a registered type mapping, see Getting Services from the SharePoint Service Locator.

Home page on MSDN | Community site