WPF Validation using Validation Application Block Part 1 – Server side

If you are interesting to learn more about Validation Application Block you can always download Hand on Labs about VAB: http://www.microsoft.com/downloads/details.aspx?FamilyID=2C34A9CB-17CF-4AEC-8DE6-EEACBBB74413&displaylang=en

In these labs you will find very good explanation about using VAB integration with WCF, but what if you are manually creating proxy instead using add service reference?

In this article I will show you how to use VAB 4.1 with WCF and manually generated proxy.

My solution structure looks like this :

WPSolution

I have created one project called DataContracts where all DataContract classes and Validation rules are defined. Code for that class looks like this :

 public class Customer

    {

        [StringLengthValidator(1, 25,

           MessageTemplateResourceType = typeof(Resources),

           MessageTemplateResourceName = “FirstNameMessage”)]

        [NotNullValidator]

        public string FirstName { get; set; }

        [StringLengthValidator(1, 25,

          MessageTemplateResourceType = typeof(Resources),

          MessageTemplateResourceName = “LastNameMessage”)]

        [NotNullValidator]

        public string LastName { get; set; }

        public string Nickname { get; set; }

        public DateTime BirthDate { get; set; }

        [ObjectValidator]

        public Address Address { get; set; }

        public Customer()

        {

            this.Address=new Address();

        }

    }

    public class Address

    {

        [StringLengthValidator(1, 50)]

        public string StreetName { get; set; }

        public int Number { get; set; }

        [DomainValidator(“AL”, “AK”, “AS”, “AZ”, “AR”, “CA”, “CO”, “CT”, “DE”, “DC”, “FM”, “FL”, “GA”, “GU”, “HI”, “ID”, “IL”, “IN”, “IA”, “KS”, “KY”, “LA”, “ME”, “MH”, “MD”, “MA”, “MI”, “MN”, “MS”, “MO”, “MT”, “NE”, “NV”, “NH”, “NJ”, “NM”, “NY”, “NC”, “ND”, “MP”, “OH”, “OK”, “OR”, “PW”, “PA”, “PR”, “RI”, “SC”, “SD”, “TN”, “TX”, “UT”, “VT”, “VI”, “VA”, “WA”, “WV”, “WI”, “WY”,

            MessageTemplate = “Value is not a valid US state two-letter abbreviation.”)]

        public string USState { get; set; }

    }

I have manually created proxy class. It looks like this :

  public class CustomerClientProxy : ClientBase<ICustomerService>, ICustomerService

    {

        public string ProcessCustomer(DataContracts.Customer customer, string notes)

        {

            return Channel.ProcessCustomer(customer, notes);

        }

    }

In service contracts project I have defined interface that WCF will use. We must decorate method signature in interface with  validation attribute that define FaultContract for method.

We are using  ValidationFault class that is defined in VAB for error transporting to the client side.

  [FaultContract(typeof(ValidationFault))]

        string ProcessCustomer(

            Customer customer, [StringLengthValidator(1, 100,

                MessageTemplate = “The notes must be 1 to 100 characters long.”)]

            string notes);

In web config file for WCF service we must add new behaviorConfiguration for Endpoint. ValidationElement is used to perform some validation actions on WCF before calling method on WCF.

<behaviorExtensions>
<add name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.ValidationElement,
        Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF, Version=4.1.0.0, Culture=neutral,
        PublicKeyToken=31bf3856ad364e35" />
</behaviorExtensions>

On client side I have one standard WPF application with basic App.Config where is just defined ABC for WCF. (address, binding, contract)

I have bind client side textboxes on the form with my Customer class.  After that I have some code to catch exceptions throw by the server and show that exceptions in simple message box.

using (Proxy.CustomerClientProxy client = new Proxy.CustomerClientProxy())

            {

                try

                {

                    Customer cust=((Customer)this.DataContext);

                    client.ProcessCustomer(cust, “My message!!!”);

                }

                catch (FaultException<ValidationFault> fex)

                {

                    StringBuilder builder = new StringBuilder();

                    builder.AppendLine(“Validation error invoking service:<br/>”);

                    foreach (ValidationDetail result in fex.Detail.Details)

                    {

                        builder.AppendLine(

                            string.Format(“{0}: {1}<br/>”, result.Key, result.Message));

                    }

                    MessageBox.Show(builder.ToString());

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.StackTrace.ToString());

                }

            }

After I run my application and enter some incorrect values I get message box :MessageBox

Reference : http://www.clariusconsulting.net/blogs/kzu/archive/2007/09/24/WhyweneedanEntLibStandaloneValidationApplicationBlock.aspx

Comments are closed.