This project is read-only.

How to validate parameters with message inspectors using schemas

J.D. Meier, Jason Taylor, Prashant Bansode, Carlos Farre, Madhu Sundararajan, Steve Gregersen

Use the followings steps to validate using message inspectors and schemas:
  1. Start with the schema created when you add a service reference inside visual studio. It is located under the service reference folder with the name of the service and XSD extension.
  2. If the service reference was created with svcutil.exe tool, you can create windows class project with the CSHARP file and then use the xsd.exe tool to generate the schema from the assembly: xsd.exe assembly.dll.
  3. Modify the schema to include schema facets to validate simple types for format type and length.
  4. Create a class the implements the schema validation logic. This class has to inherit from the interfaces IClientMessageInspector and* IDispatchMessageInspector. IClientMessageInspector* is only needed if you want to do client side validation. IDispatchMessageInspector is only needed if you want to do service side validation.
  5. Implement the methods IDispatchMessageInspector.AfterReceiveRequest IDispatchMessageInspector.BeforeSendReply, IClientMessageInspector.AfterReceiveReply, IClientMessageInspector.BeforeSendRequest depending if you are validating inbound or outbound in client / service.
  6. Create a class that implements a endpoint behavior. This class has to inherit from interface IEndpointBehavior.**
  7. Implement the methods ApplyClientBehavior and ApplyDispatchBehavior depending if you are doing client and/or service side validation. Add the inspectors in with clientRuntime.MessageInspectors.Add in ApplyClientBehvior and/or DispatchRuntime.MessageInspectors.Add in ApplyDispatchBehavior
  8. Create a class that implements custom behavior element. This class has to inherit from BehaviorExtensionElement. This will allow adding the custom behavior to the configuration tool.
  9. Add the Custom Behavior to the Configuration File. Insert the custom extension inside the WCF configuration file, so it can be used by the service
<system.serviceModel>
  ...
<extensions>
      <behaviorExtensions>
        <add name="SchemaValidator" 
         type="MySchemaValidationClass.SchemaValidation+CustomBehaviorSection, 
         MySchemaValidationClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
 ... 
 <system.serviceModel>
  1. Create an Endpoint Behavior and Map it to Use the Custom Behavior. This will allow the endpoint behavior to use the schema valiation
<behaviors>
  ...
<endpointBehaviors>
        <behavior name="MyEndPointBehavior">
          <SchemaValidator />
        </behavior>
      </endpointBehaviors>
 ...  
 </behaviors>
  1. Configure the Service Endpoint to Use the Endpoint Behavior. This will allow the service endpoint to be configured with the endpoint behavior
<endpoint address="" behaviorConfiguration="MyEndPointBehavior"
          binding="wsHttpBinding" contract="IService">
          <identity>
            <dns value="localhost" />
          </identity>
</endpoint>

Last edited May 8, 2008 at 7:21 AM by prashantbansode, version 1

Comments

No comments yet.