VAB + WCF + ASP.net = ?

Topics: Validation Application Block
Oct 23, 2008 at 3:29 PM
Hello!

Now I'm trying to use the validation block applying the N-tier model and exposing the validation trough a WCF Service.

I'm following this example: http://www.codeproject.com/KB/WCF/WCFVAB.aspx

I created this code:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><cc1:PropertyProxyValidator
            ID="PropertyProxyValidator1" runat="server" ControlToValidate="TextBox1" PropertyName="Amount" SourceTypeName="Mex.OrderData"></cc1:PropertyProxyValidator><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><cc1:PropertyProxyValidator
            ID="PropertyProxyValidator2" runat="server" ControlToValidate="TextBox2" PropertyName="Currency" SourceTypeName="Mex.OrderData"></cc1:PropertyProxyValidator><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>

But it didn't work as I expected.

I guess that the problem is get the validation result that is returned as an exception, because even when I enter an invalid data the Page.isValid returns true.

Does anyone have an example about how to use propertyproxyvalidator with wcf + VAB? Is it possible or Do I have to call the validation programatically?

Regards,
Roberto O Santos
www.robertoosantos.com
Oct 23, 2008 at 6:12 PM
Hi Roberto,

The PropertyProxyValidator is an ASP.NET validator, and it validates ASP.NET's controls' values as part of a page's life cycle.; what does your integration with WCF look like, and how did you expect it to work?

Regards,
Fernando
Oct 23, 2008 at 6:32 PM
Edited Oct 23, 2008 at 6:37 PM
Hello Fernando,

I did it like this:

protected void Button1_Click(object sender, EventArgs e)

    {

        try

        {

            // Create an instance of the service proxy

            OrdersServiceClient proxy = new OrdersServiceClient();



            // This method has no validations in it. It should be OK

            OrderData orderData = new OrderData();

            orderData.Amount = Double.Parse(TextBox1.Text); ;

            orderData.Currency = TextBox2.Text;

            OrderInfo info = proxy.CreateOrderWithDataContractValidation(orderData);



            // The code should never reach here if the validation is enabled

            Label1.Text = "Valid!";

        }

        catch (FaultException<ValidationFault> ex)

        {

            ValidationFault faults = ex.Detail;



            // Iterate through the list of validation errors

            foreach (ValidationDetail validationResult in faults.Details)

            {

                switch (validationResult.Key)

                {

                    case "Amount":

                        PropertyProxyValidator1.ErrorMessage = validationResult.Message;

                        PropertyProxyValidator1.IsValid = false;

                        break;

                    case "Currency":

                        PropertyProxyValidator2.ErrorMessage = validationResult.Message;

                        PropertyProxyValidator2.IsValid = false;

                        break;

                    default:

                        Label1.Text = "Invalid!";

                        break;

                }

            }

        }

    }


In this scenario the asp.net web page calls the WCF which is responsable for the validation... This way I have may validation logic apart of my User Interface Process... It became a kind of Business Rule. And also it's not necessary to have the Business Dll's at the same layer as the asp.net pages.

Now I'm going in another way... Instead of use the propertyproxyvalidator and the programing on the buttom click I'll try to use the Asp.net customvalidator to call the WCF and the validation using the ServerValidate event and the ClientValidationFunction property to call a JavaScript validation.

I guess that this is a more elegant solution. What do you think about it?

Regards,
Roberto O Santos
www.robertoosantos.com
Oct 23, 2008 at 8:41 PM

Check out http://msdn.microsoft.com/en-us/library/ms178472.aspx. The PropertyProxyValidator works during the validation stage, before event handlers are invoked, and validates a single control.

You could try instead setting up a CustomValidator with a handler that would collect the controls' values and invoke the web service. With this approach you wouldn't have a per-control error message but it would fit better with the need to perform a single web service call IMO.

Hope this helps,

Fernando