Introduction to Enterprise Library: Part VIII

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>();

        public void AddEmployee(Employee objEmp)

        {

            empList.Add(objEmp);

        }

 

        public Employee GetEmpDetails(int id)

        {

    return empList.Find(e => e.EmpID.ToString().Equals(id.ToString()));

        }

 

        #endregion

    }
}

Here, we are validating parameter range for GetEmpDetails and attribute-based validation on EmpName.

Build the solution and run/deploy the service. Now, we need to create a client for testing this service. So, create a new console application and name it as WCFAdapterVABClient. Add service reference of the WCFAdapterVAB and write the below code in Program.cs:

using Microsoft.Practices.EnterpriseLibrary.Validation;

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF;

using System.ServiceModel;

 

namespace WCFAdapterVABClient

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                   EmpService1.EmpServiceClient service1 = new
                            WCFAdapterVABClient.EmpService1.EmpServiceClient();

                   EmpService1.Employee objemp = 
                            new
 WCFAdapterVABClient.EmpService1.Employee();

                   objemp.EmpID = 15;

                   objemp.EmpName = "c-sharpcorner";

                   service1.AddEmployee(objemp);

                   EmpService1.Employee emp = service1.GetEmpDetails(15);

                   EmpService1.Employee objemp1 = service1.GetEmpDetails(150);

          }

            catch (FaultException<ValidationFault> ex)

            {

            ValidationFault fault = ex.Detail;

           foreach (ValidationDetail validationResult in fault.Details)

                {

                    Console.WriteLine(validationResult.Message);

                }

                Console.ReadLine();

            }

        }

    }

}

 
Run the client application, the output will be like this:

 

Suggestion:

When we debug WCF service having VAB  validation, sometimes we may get unhandled  user exceptions; to handle that goto VS  Debug  Exceptions  Uncheck User-unhandled checkbox  under CLR Exceptions for ServiceModel  and other required classes.

I am ending the things here. I am attaching source code for reference. In coming articles, we will go deep into this Application block. I hope this article will be helpful for all.


Similar Articles