WCF Validations At Service Level

From this article we will discuss about validations in WCF at service level. Extensibility is one of the main features in WCF. There is no any predefined Validations block in WCF as MVC like [Required].

But we can give such facility to your service at service level with various packages. I will explain you with small scenarios.

itestservice

In the above screen, we mentioned [Required] attribute to the ‘sname’ parameter in Add() method. But it won’t work at service level.

Why it won’t work?

  1. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]  
  2. public class RequiredAttribute: ValidationAttribute  
The [Required] attribute didn’t have any usage property for Interfaces. It allows working for only field, property and Parameter.

Then how to implement Validations at Service level

Integration Validation blocks and Integration WCF Validation Packages arrived to implement such feature to WCF Service.

For that we need to install some packages to your service.

Install-Package EnterpriseLibrary.Validation.Integration.WCF && Install-Package EnterpriseLibrary.Validation

wcf validation

Let’s come into service side and do the changes accordingly.

itestservice

Let’s test your Service. It is working fine.

testservice

But it has some limits. Methods and Method types are not same. Find the following screen if your service have sync methods.

methods

Now let’s test service again with sync methods and integration WCF Validations block.

testservice

Finally, again a nuget package arrived to solve this problem for sync and all methods. Find the following screen for solution.

Use this below one Instead of two Install-Package EnterpriseLibrary.Validation.Integration.WCF, Install-Package EnterpriseLibrary.Validation.

custom validation

Now all the recommended packages arrive to your solution.

solution

And let’s test your service and expose it.

testservice

Finally, WCF Validations supports your sync methods and non-sync methods at service level.