VAB and WCF Cnnot get it to work EL 6.0 VS2012 Win8

Topics: Validation Application Block
Sep 25, 2013 at 5:01 PM
I cannot get this to work it is simply not validating. I have no idea why. Please help.
  1. WCF Service
namespace WcfServiceVAB
{
[ServiceContract]
[ValidationBehavior]
public interface IService1
{
    [OperationContract]
    [FaultContract(typeof(ValidationFault))]
    string TestHeader(Models.Header newHeader);
}
}

namespace WcfServiceVAB
{
 public class Service1 : IService1
{
    public string TestHeader(Models.Header newHeader)
    {
        Logic.GetHeaderTest sv = new Logic.GetHeaderTest();
        return sv.TestHeader(newHeader);
    }
}
}

namespace WcfServiceVAB.Models
{
[DataContract]
public class Header
{
    [DataMember]
    private int? CompanyID;
    public int? bpCompanyID
    {
        get { return CompanyID; }
        set { CompanyID = value; }
    }

    [NotNullValidator(MessageTemplate = "Start Effective Date cannot be null")]
    [DataMember]
    private DateTime? StartEffectiveDate;
    public DateTime? bpStartEffectiveDate
    {
        get { return StartEffectiveDate; }
        set { StartEffectiveDate = value; }
    }

    [NotNullValidator(MessageTemplate = "End Effective Date cannot be null")]
    [PropertyComparisonValidator("bpStartEffectiveDate",
                             ComparisonOperator.LessThanEqual, MessageTemplate = "EndEffectiveDate cannot be before StartEffectiveDate")]
    [DataMember]
    private DateTime? EndEffectiveDate;
    public DateTime? bpEndEffectiveDate
    {
        get { return EndEffectiveDate; }
        set { EndEffectiveDate = value; }
    }


    [StringLengthValidator(2,5,MessageTemplate = "Description cannot be null")]
    [DataMember]
    private string Description;
    public string bpDescription
    {
        get { return Description; }
        set { Description = value; }
    }      
}
}

namespace WcfServiceVAB.Logic
{
public class GetHeaderTest
{
    public string TestHeader(Models.Header header)
    {
        return header.bpDescription;
    }
}
}

<?xml version="1.0"?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
<!--<authentication mode="Windows"/>
<identity impersonate="false"/>-->
</system.web>

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="WcfServiceVABbinding" maxBufferPoolSize="2147483647"
      maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

<services>
  <service behaviorConfiguration="WcfServiceVABbehaviour" name="WcfServiceVAB.Service1">
    <endpoint address="" behaviorConfiguration="ValidationBehavior"
      binding="basicHttpBinding" contract="WcfServiceVAB.IService1" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:49475" />
      </baseAddresses>
    </host>
  </service>
</services>

<behaviors>

  <endpointBehaviors>
    <behavior name="ValidationBehavior">
      <validation enabled="true"/>
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="WcfServiceVABbehaviour">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<!--<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>-->   
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
<extensions>
  <behaviorExtensions>
    <add name="validation"
               type="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.ValidationElement, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF, version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </behaviorExtensions>
</extensions>

</system.serviceModel>

<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>
  1. Client
Standart Client app.config
Standart ServiceReference

namespace TestClientConsole
{
class Program
{
    static void Main(string[] args)
    {
        ServiceReference1.Service1Client Client = new ServiceReference1.Service1Client();

        ServiceReference1.Header item = new ServiceReference1.Header();


        using (Client)
        {
            try
            {
                item.CompanyID = 1;
                item.StartEffectiveDate = Convert.ToDateTime("01/01/2013");
                item.EndEffectiveDate = Convert.ToDateTime("01/01/2013");
                item.Description = "";

                Client.TestHeader(item);

            }
            catch (FaultException<ValidationFault> ex)
            {
               // ValidationFault fault = ex.Detail;
                foreach (ValidationDetail validationResult in ex.Detail.Details)
                {
                    Console.WriteLine(validationResult.Message);
                }
                Console.ReadLine();
            }
            catch (FaultException ex)
            {
                Console.WriteLine(ex.Message);

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }

        }
    }
}
}

I will appreciate any help.
Thank you,

GrazynaW
Sep 27, 2013 at 1:03 AM
Thanks for posting a detailed example.

The reason why validation is not occurring is that the validation attributes are applied to private fields. Validators only validate public readable properties and public fields. If you move the attributes to the public properties then it should work.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 10/7/2013 at 10:54 PM
Sep 27, 2013 at 8:03 PM
Thanks,
Yes moving properties to public ones fixed the problem.

GrazynaW