Using Enterprise Library Validation Application Block in WCF

Validation plays an important role in any messaging communication between distributed systems and especially when you are designing a SOA where the service is consumed by many third party consumers.

In this post I will show how to leverage the Enterprise Library Validation Application Block to apply validation on WCF Service contracts that really makes the task very easy for the developers to apply validations.

Develop Service
I will create a simple project that shows how one can apply validation on Service contracts in WCF.

  1. First of all create a Simple WCF Service Application Project in Visual Studio
  2. Add a reference to Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF assembly.

  3. Open the Service Interface (by default it generated IService) and apply the class level attribute “ValidationBehavior” on service’s interface. ValidationBehavior is Validation Application Block Service Behavior and is responsible to validate requests. Now you need to apply the FaultContract and specify the type of data that holds the information about the error.

            FaultContract is used to communicate error from service to client.
     [ServiceContract]

     [ValidationBehavior]
     public interface IService1

 {
   [OperationContract]

   [FaultContract(typeof(ValidationFault))]
   string Execute(Name value);
 }
In the above code you can see there is a Name class object passed as a parameter in Execute    function.

     4.  Now implement the Service Contract interface in HelloWorldService class.

    public class HelloWorldService :IService1{
      public string Execute(Name name){
        return string.Format(“your name is {0} {1}”, name.FirstName, name.LastName);
      }
    }

5. This Name class is the Service’s DataContract. Below is the code snippet for Name class

    [DataContract]
    public class Name {
      [StringLength(25, MinimumLength=1, ErrorMessage=“First Name length is not correct”)]
      [DataMember]
      public String FirstName{ set; get; }

      [StringLength(25, MinimumLength = 1, ErrorMessage = “Last Name length is not correct”)]
      [DataMember]
public string LastName { set; get; }

}
In the above code, I have specified two properties of String type and applied Data Annotations attribute for String length validation. You can apply as many validations as you want.

Consume Service

6. Create a sample console project in Visual Studio to test.

7. Add a service reference by clicking on the Service References in Visual Studio.

8. Initialize service instance

9. Call Execute function and pass invalid string values.

   static void Main(string[] args) {
      var name = new Name { FirstName = “”, LastName = “” };
      var client = new ConsoleApplication1.Services.Service1.Service1Client();
      try{ Console.WriteLine(client.SayHello(name)); }
catch (FaultException<ValidationFault> fault)
      {
         foreach (var detail in fault.Detail.Details)
{
               Console.WriteLine(“{0}:{1}”, detail.Key, detail.Message); }
      }
      client.Close();
      Console.ReadLine();
    }

10. In the above code I have passed Name object with empty string values to test whether the service is validating or not and here is the result.

Advertisements

2 thoughts on “Using Enterprise Library Validation Application Block in WCF

  1. how can get the exception detail from a java or diferent client. it doesn’t return an xml error message, appears an noncontrolled exception on server. thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s