customizing VALIDATIONFAULT in WCF

Topics: Validation Application Block
May 25, 2010 at 8:35 AM

Hi Experts,

I created one Datacontract. I have put one Validation lets say SSNValidator On the following DTO
<pre>
[DataContract]
public class ClientValidDTO
   {


       [DataMember]
       public string Name
       {
           get;
           set;
       }

       [DataMember]
       [SSNValidator(false)]
       public string SSN
       {
           get;
           set;
       }</pre>

i have created one WCF service with one method ConcateClientData
which will take Client Datacontract as a Parameter.

i.e.
<pre>[ServiceContract]
[ValidationBehavior]
public interface IClientValidSrv
{
    [OperationContract]
    [FaultContract(typeof(ValidationFault))]
    ConcateDTO ConcateClientData(ClientValidDTO client);
}</pre>

At Client side when i catch exception i got ValidationResult as a fault
i.e.

<pre>
private void btnSave_Click(object sender, EventArgs e)
       {
           ClientValidSrv.ClientValidSrvClient proxyClient = new PresentationLayer.ClientValidSrv.ClientValidSrvClient();

           try
           {
               ClientValidSrv.ClientValidDTO objClientDTO = new ClientValidSrv.ClientValidDTO();
               objClientDTO.Name = txtName.Text;
               objClientDTO.SSN = txtSSN.Text;
               objClientDTO.State = txtState.Text;

               ClientValidSrv.ConcateDTO objContateDTO = proxyClient.ConcateClientData(objClientDTO);
               label3.Text = objContateDTO.FullName;
           }
           catch (FaultException&lt;ClientValidSrv.ValidationFault&gt; ex)
           {
               DisplayFault(ex.Detail);
           }
           catch (FaultException ex)
           {
               MessageBox.Show(ex.Message);
           }
           finally
           {
               proxyClient.Close();
           }

       }

        private static void DisplayFault(ClientValidSrv.ValidationFault faults)
       {
          MessageBox.Show(&quot;Fault Occured:&quot;);
           foreach (ClientValidSrv.ValidationDetail validationResult in faults.Details)
           {
             MessageBox.Show(string.Format(&quot;Message={0} Key={1} Tag={2}&quot;, validationResult.Message, validationResult.Key, validationResult.Tag));

           }
       }</pre>


ValidationResult has Message,Key,Tag as default properties.
How can i add additional property ErrorId in validationResult to show customize error message at client side?

May 26, 2010 at 12:45 AM

You can't unless you modify the code.  Why not simply make use of its Tag property?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 26, 2010 at 3:52 AM

Hi,

 

I have tried in my custom validator to set Tag property

 

i.e.

 

 

 

ValidationResult val = new ValidationResult("Invalid Area 101", currentTarget, "SSN Number", "101", null);

 

 

or

 

in Datacontract

 

 

 

 

 

[

 

DataMember

]<font size="2"> </font>

[

 

 

StringLengthValidator(1,20,MessageTemplate="String should be Between 1 to 25 chars",Tag="99"

)]<font size="2"> </font>

 

 

public string

Name

{

 

 

 

get

;<font size="2"> </font>

 

 

set

 

;

}

 

 

but at client side it shows Tag=client. For message and key it is setting the value

i.e.

 

Key "SSN Number" string
Tag "client" string

Message "Invalid Area 101" string

I tried in winforms and Tag property is working fine but in from WCF it is not working.

Can you please guide me on this?

Thanks,

Murtuza

 

 

 

 

May 26, 2010 at 4:07 AM

Turns out the ValidationParameterInspector replaces the Tag property with the name of the parameter.  Sorry, didn't know this before. 

Looks like there are no other properties available that would do the trick.  Currently, the only way I see to get through right now is to modify the source code but I don't think you would do that just so you could add a property.  I'll try to think of another way and get back to you.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com