DCSIMG
Validating WCF Service Messages with Validation Application Block - Part 1 - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

Validating WCF Service Messages with Validation Application Block - Part 1

This post is one of a post series about WCF Integration in Enterprise Library 3.0.

This is the first post that I am writing about the integration between Windows Communication Foundation and Validation Application Block in Enterprise Library 3.0. This post will focus on what you should do in order to declare the validation on parameters of service operations.

When you want to make sure that the service operation receives valid parameters, you can achieve that in two ways:

1. Validating Parameters on a service operation

If your service operation received parameters with primitive types, such as:

[OperationContract]

int CreateOrder(string currency, double amount);

and you still want to validate them, you can use the parameter-level validation in the service contract:

[ServiceContract]

public interface IOrdersService

    [OperationContract]

    int CreateOrder(

        [NotNullValidator] string currency,

        [RangeValidator(1.0, RangeBoundaryType.Inclusive, 2.0, RangeBoundaryType.Inclusive)] double amount);

}

Notice the usage of the validation attributes before each of the parameters of the service operation.

2. Validating Message Contracts or Data Contracts passed to a service operation

If your service operation received Data Contract parameter or a Message Contract parameter, you actually use the validation logic specified above them. For example, this a Data Contract of order data that has validation logic that make sure that the input currency is one of a pre-defined domain of values.

[DataContract]

public class OrderData

    [DataMember]

    public double Amount

    {

        get { return amount; }

        set { amount = value; }

    }

 

    [DataMember]

    [DomainValidator("USD","EUR","JPY")]

    public string Currency

    {

        get { return currency; }

        set { currency = value; }

    }

}

Now, when you use it in a service contract definition, you don't have to specify the validation for the input parameters, since it will be taken from the Data Contract definition.

[OperationContract]

OrderInfo CreateOrder(OrderData orderData);

Keep in mind that in order to validate an object, using attributes is not the only way to do so. If you need more flexibility you can do it using the configuration file.

In this post I talked about what you have to do in order to define what kind of parameters validation you want to achieve in your WCF service operations. In the next post I will talk about what happens when the parameters are not valid.

Enjoy!

Comments

Massimo said:

Hi Guy.

I had troubls days ago making work WCF vith VAB.

At the beginning I used the validation framework integrated with WCF to validate my service messages and my message/data contracts.

Here follows the definition for the service

  [ServiceContract]

  [ValidationBehavior]

  public interface IService

     [OperationContract]

     [FaultContract(typeof(ValidationFault))]

     MyResponse Search([Validator(typeof(InstanceValidator))]MyRequest req);

   .....

  and here follows the MyRequest class

  [MessageContract]

  public class MyRequest

  {

     private String id;

     [MessageBodyMember(Order = 0)]

     [Validator(typeof(LengthValidator), 9, 9, "Not valid lenght")]

      public String Id

       {

           get { return id; }

           set { id = value; }

       }

   }

This old way worked fine, but since the Validation Application Block was released I decided to change the validation framework.

So I made the necessaries changes:

To the ServiceContract:

  [ServiceContract] // removed the old [ValidationBehavior]  

  public interface IService

     [OperationContract]

     MyResponse Search(MyRequest req);

     // removed [FaultContract(typeof(ValidationFault))]

     // remove [Validator(typeof(InstanceValidator))] in front of the request

......

and to the MessageContract

[MessageContract]

  public class MyRequest

  {

     private String id;

     [MessageBodyMember(Order = 0)]

       // removed [Validator(typeof(LengthValidator), 9, 9, "Not valid")]

      [NotNullValidator]

      [StringLengthValidator(9, 9)]

      public String Id

       {

           get { return id; }

           set { id = value; }

       }

   }

It seems to compile and works all great, but when we invoke the service we got no validation.

It seems that the WCF does not recognize the Validation Application Block.

Any idea?

Sincerely

Massimo Ugues

# June 6, 2007 3:28 PM

גיא בורשטיין said:

hi,

I didn't understand the whole picture. Please contact me by mail. I'd like to help...

Guy

# June 6, 2007 3:57 PM

Frank said:

Troubleshooting WCF Podcast show #94 WCF WCF home Config WCF for unit testing Validating WCF Service

# June 18, 2007 2:23 AM

Guy Burstein's Blog said:

I've posted about the WCF Integration with Validation Application Block and the integration with Exception

# June 19, 2007 7:22 PM

Guy Burstein's Blog said:

Learn how to integrate WCF with Enterprise Library (Logging, Validation and Exception Handling)

# July 24, 2007 3:54 PM

Nabheet Sandhu said:

Hi,

I have the same problem that Massimo Ugues ran into (I think). Whenever i use a message contract as input for the validatorfactory.create validator method, it does not create any nested validators.

Can you please help?

thanx

Nabheet

# October 11, 2007 7:07 PM

Nabheet Sandhu said:

Hi,

I think i may have found a possible problem with my setup. The MessageContract and DataContracts are in separate projects. When I create a validator on the MessageContract I don't get any nested validators for the DataContracts, but when I use a DataContract I get all the nested validators. Can the validationfactory.createvalidator method create validators across dlls.

Nabheet

# October 11, 2007 7:26 PM

Eric Hauser’s Blog » Blog Archives » Validations with WCF and JSON said:

Pingback from  Eric Hauser’s Blog  » Blog Archives   » Validations with WCF and JSON

# November 29, 2007 4:06 AM

ram said:

Hi,

How to test the porperty by using the Nunit

# January 19, 2009 9:11 AM

ram said:

Hi,

How to test the porperty by using the Nunit

# January 19, 2009 9:11 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: