WCF Data Services: The Processing Pipeline

Description and code samples
Description

On .NET 4.x  Framework the processing pipeline is encapsulated on the DataServiceProcessingPipeline class and can be manipulated by the developer.

The DataServiceProcessingPipeline class is used as a property of the DataService<T> class and exposes four events:

  • ProcessingRequest – The event occurs before a request to the data service is processed.
  • ProcessingChangeset – The event occurs before a change set request to the data service is processed.
  • ProcessedRequest – The event occurs after a request to the data service has been processed.
  • ProcessedChangeset – The event occurs after a change set request to the data service has been processed.

Injecting additional logic through the pipeline

The configuration of the events to handle is done on service constructor event handlers. Through this handlers generic behaviour and business logic can be added to the service:

public class SchoolDataService : DataService<SchoolEntities>
{
  public SchoolDataService()
  {
    ProcessingPipeline.ProcessedChangeset += new EventHandler<EventArgs>(ProcessingPipeline_ProcessedChangeset);
    ProcessingPipeline.ProcessedRequest += new EventHandler<DataServiceProcessingPipelineEventArgs>(ProcessingPipeline_ProcessedRequest);
    ProcessingPipeline.ProcessingChangeset += new EventHandler<EventArgs>(ProcessingPipeline_ProcessingChangeset);
    ProcessingPipeline.ProcessingRequest += new EventHandler<DataServiceProcessingPipelineEventArgs>(ProcessingPipeline_ProcessingRequest);
  }

  void ProcessingPipeline_ProcessingRequest(object sender, DataServiceProcessingPipelineEventArgs e)
  {
    Debug.Write("Processing Request was called");
  }

  void ProcessingPipeline_ProcessingChangeset(object sender, EventArgs e)
  {
    Debug.Write("Processing Change Set was called");
  }

  void ProcessingPipeline_ProcessedRequest(object sender, DataServiceProcessingPipelineEventArgs e)
  {
    Debug.Write("Processed Request was called");
  }

  void ProcessingPipeline_ProcessedChangeset(object sender, EventArgs e)
  {
    Debug.Write("Processed Change Set was called");
  }

  public static void InitializeService(DataServiceConfiguration config)
  {
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  }
}

 

References

[1] The WCF Data Services pipeline description