Thursday, June 06, 2013

Instrumenting WCF, Extending WCF using IServiceBehavior, IOperationBehavior, and IParameterInspector

Introduction 

WCF has become such a big buzz word in IT solution world, it revolutionary transfers the web services concept to a totally new world, it perfectly implements and extends the Service Oriented Architecture and it also opens the ways to freely design and build flexible solutions.

Background

 My intent was to talk about a very important feature of the WCF and that was extensibility, however, I found a very good series of articles online that talk about extensibility in a very detailed and simple professional way that I can't ever compete with, so I decided to briefly talk about extensibility referring the reader to that great article by Carlos Figueira MSDN blog http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx.

 In the lines below I am going to show you how you can extend WFC  in a very simple way using three extending points, and in case you want to know more, you can just read the link above. 

Using the code 
This article requires you to be well-versed with WCF's main concepts like service, contracts, behaviors bindings etc. 

I will build a simple WCF service I will extend the service in three places, or show you how you can extend the service in three places and also show you the execution path by tracing it. Let's start by considering this simple contract:

[ServiceContract(Name = "PasswordGenerator")]
public interface IPasswordGenerator
{
    [OperationContract(Name = "GeneratePassword")]
    string GeneratePassword();
    [OperationContract(Name="ParameterizedPasswordGenerator")]
    string GeneratePassword(int length);
}

And this implementation:

public class PasswordGenerator:IPasswordGenerator
{
    [OperationBehavior()
    public string GeneratePassword()
    {
        return "You called GeneratePassword()";
    }



    [OperationBehavior()]
    public string GeneratePassword(int length)
    {
        return "You called GeneratePassword(int length) overload";
    }
}


Read more: Codeproject
QR: Inline image 1