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

Testservice

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

package manager

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

itestservice

Let’s Test your Service. And it is working fine.

testservice

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

methods

Now let’s Test service again with sync methods and Integration WCF Validations block.

wcf

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

Now all the recommended packages arrive to your solution.

solution

And let’s test your service and expose this service.

service

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

Up Next
    Ebook Download
    View all
    Learn
    View all