Click here to Skip to main content
15,886,840 members
Articles / Programming Languages / C#

WCF Data Services Processing Pipeline

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
2 May 2011CPOL2 min read 19.1K   4   1
A new feature inside WCF Data Services that enables to wire up events into the service’s processing pipeline.

First I must confess.WCF Data Services Processing Pipeline Even though I like OData and WCF Data Services, in the last couple of months I didn’t have the chance to work or use them. This is why when .NET 4 was shipped, I didn’t notice a new and interesting extension point in the framework – the processing pipeline. In the last MIX11, I got a little introduction to that extension point in Mike Flasko’s session. In this post, I’ll explain the WCF Data Services' server side processing pipeline.

WCF Data Services Processing Pipeline

In the past, I wrote about the Interceptions mechanism that is built inside WCF Data Services. One of the advantages of using that mechanism is the ability to wire up business logic such as custom validations, access policy logic, or whatever you like to insert in the queries/change sets pipeline. The problem starts when you need a generic behavior for all the entity set interceptions. In the first release of WCF Data Services, there were no extension points to use that would help implement generic things. But now, in .NET4, you can use the processing pipeline in order to do that. The new DataServiceProcessingPipeline is a class that is being used as a property of the DataService class (which every data service inherits from). It exposes four events that you can hook to:

  • 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.

These events enable the developer to write code that is performed during the process of WCF Data Services pipeline before and after things happen.

Using the WCF Data Services Processing Pipeline

Here is a simple example of how to use the processing pipeline events to write to the output window that the events were called:

C#
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;      
  }
}

As you can see in the service constructor, I wire up all the events. This is a simple sample but as I wrote, you can put in the event handler logic that performs generic behaviors such as security policies, validations, and etc.

Summary

In .NET 4, there is a new feature inside WCF Data Services that enables to wire up events into the service’s processing pipeline. This is very helpful in scenarios such as business logic that you want to impose on the service execution behavior.

This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
GeneralMy rating 4 Pin
SVV Sukumar13-Sep-11 8:22
SVV Sukumar13-Sep-11 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.