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.
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
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
- 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
Let’s come into service side and do the changes accordingly.
Let’s Test your Service. And it is working fine.
But it has some limits. Methods and Method types are not same. Find the following screen, if your service is having sync methods.
Now let’s Test service again with sync methods and Integration WCF Validations block.
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.
Now all the recommended packages arrive to your solution.
And let’s test your service and expose this service.
Finally, WCF Validations supports to your sync methods and non-sync methods at service level.