In previous article, we had discussed about Win Forms validation using VAB adapter. In this article, we will look into WCF adapter.
Create a new WCF Service Library project in VS 2008 and name it as WCFAdapterVAB. Now add the following dlls present in Enterprise Library installation folder:
- Microsoft.Practices.EnterpriseLibrary.Common,
- Microsoft.Practices.EnterpriseLibrary.Validation.Validators and
- Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF
Now, delete all the existing class files in the project. We need to add DataContract, ServiceContract for Service. Add a class file named as Employee.cs for DataContract with below code:
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF;
namespace WCFAdapterVAB
{
[DataContract]
public class Employee
{
[DataMember]
public int EmpID { get; set; }
[DataMember]
[StringLengthValidator(1, RangeBoundaryType.Inclusive, 10, RangeBoundaryType.Inclusive, MessageTemplate = "Name must be between 1 and 10.")]
public string EmpName { get; set; }
[DataMember]
public double Salary { get; set; }
[DataMember]
public string EmpMailID { get; set; }
}
}
Add another class named as IEmpService.cs for ServiceContract with below code:
namespace WCFAdapterVAB
{
[ServiceContract]
[ValidationBehavior]
public interface IEmpService
{
[OperationContract]
[FaultContract(typeof(ValidationFault))]
void AddEmployee(Employee objEmp);
[OperationContract]
[FaultContract(typeof(ValidationFault))]
Employee GetEmpDetails([RangeValidator(1,RangeBoundaryType.Inclusive,100, RangeBoundaryType.Inclusive,MessageTemplate="ID should be between 1-100")]int id);
}
}
We need to specify FaultContract property for methods throwing validation errors.
Now, we will implement the service methods in another class file named as EmpService.cs with below code:
namespace WCFAdapterVAB
{
public class EmpService : IEmpService
{
#region IEmpService Members
private List<Employee> empList = new List<Employee>();

Join the conversation! Your thoughts help the community grow.