Saturday, May 19, 2007

Getting References to Services

The term service describes a component that implements some infrastructure-related functionality that other components in the application may need to use; for example, services providing authorization, logging, or hardware communication functionality.

References to services are obtainable either programmatically or declaratively.


I. To obtain a reference to a service programmatically

  1. Call the Get method of the Services collection of the WorkItem, passing in the type of service as shown in the following code.
IMyService myServ = (IMyService)myWorkItem.Services.Get(typeof(IMyService));

// or using generics
IMyService myServ = myWorkItem.Services.Get();
II. To obtain a reference to a service declaratively
  1. Add the ServiceDependency attribute to a property in your class that that is of the type of service or interface you require, as shown in the following code.

    private IMyService service;

    [ServiceDependency]
    public IMyService MyService
    {
    set { service = value; }
    }



No comments: