Enterprise library validation block is generating exception when it is used with WF4.0

Topics: Validation Application Block
May 25, 2012 at 5:23 AM

Enterprise library validation block is not working with WF4.0 workflow service.  When we configure it with WF4,0 workflow service is unable to start.

For more details you can refer this link .

http://social.msdn.microsoft.com/Forums/en/wfprerelease/thread/2f2198d0-ae06-40ab-abe8-0e52ee0249b2

Thanks....

May 25, 2012 at 7:01 AM

For the record the posted configuration was:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Service1" behaviorConfiguration="PublishMetadata">
        <endpoint address=""
        binding="basicHttpBinding" contract="ITestService" behaviorConfiguration="Validation"/>       
      </service>      
      </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="Validation">
          <validation enabled="true"/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="PublishMetadata">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <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=null" />
      </behaviorExtensions>
    </extensions>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

And the error is:

Server Error in '/' Application.
Value cannot be null. Parameter name: operation.SyncMethod Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: operation.SyncMethod

It looks like you are using Validation with WCF.  You said the above error occurs when simply browsing the service metadata.

Are you using a custom build of Enterprise Library?  The reason I ask is because the configuration indicates a non-signed assembly.  Usually the type would be 

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

This is the configuration I'm successfully using:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Service1">
        <endpoint address="http://localhost:59355/"
        binding="basicHttpBinding" contract="ValidationWcfBaseClass.IMyService" />
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="Validation">          
          <validation enabled="true" ruleset="MyRuleset"/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <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>
    </extensions>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

And the service code is:

    public class Service1 : IMyService
    {
        public bool ExecuteCommand(CommandBase command)
        {
            return true;
        }
    }

    [ValidationBehavior]
    [ServiceContract]
    public interface IMyService
    {
        [FaultContract(typeof(ValidationFault))]
        [OperationContract]
        bool ExecuteCommand(CommandBase command);
    }

    [DataContract]
    public class CommandBase
    {
        [DataMember]
        [StringLengthValidator(5)]
        public string Name
        {
            get;
            set;
        }
    }

At runtime the service will need to access to the following assemblies:

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Validation.dll
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.Unity.dll

If that doesn't help I would recommend posting some code that demonstrates the issue.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 25, 2012 at 7:57 AM

Hi Randy,

Thanks for your replay.

I am using Enterprise library Validation block with WF 4.0 (Windows Forkflow foundation ) not for WCF, for WCF service it is working well but for WF service it it not working.

It is generating exception here. 

 

private bool HasValidationAssertions(OperationDescription operation)

 {

 MethodInfo

methodInfo = operation.SyncMethod;  

 

if (methodInfo == null )

{ 

 

throw new ArgumentNullException("operation.SyncMethod"

);

}

 

return methodInfo.GetCustomAttributes(typeof(ValidatorAttribute), false

).Length > 0 || HasParametersWithValidationAssertions(methodInfo.GetParameters());

}

 in class   public class ValidationBehavior

Please help me to resolve this issue.

May 25, 2012 at 8:37 AM

Are you using an Asynchronous service?  I don't believe asynchronous services are supported.  See http://entlib.codeplex.com/discussions/31493 for discussion and a potential code change to Enterprise Library to support async.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

May 25, 2012 at 8:51 AM

Thanks,

Yes i am using Async one. I have implemented the changes suggested on that link but no luck.

operation.SyncMethod and  operation.BeginMethod both return null for WF4.0 services. Is there any way to resolve this issue?

I want to use validation block to validate request parameter.

May 28, 2012 at 6:42 PM

The fix in the article does work for async services.  However, WF services seem to be a bit different.  I didn't see a good way to determine what method called was by examining OperationDescription which makes it difficult to make the existing implementation work.  I'm no WF expert (</understatement>), but it looks to me like you may need an entirely different implementation that is workflow specific.

An alternative might be to provide a custom WCF facade (which gives you full control) over the workflows.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com