Share via


How to: Create and Add a Custom Input Filter

Input filters are used by the Web Services Enhancements for Microsoft .NET (WSE) to process the SOAP envelope of an incoming message. Several filters are built into WSE for features such as security and routing. You can also create a custom filter or a collection of input filters that can be applied to any incoming message. This collection of custom filters is added to the collection of filters returned by the InputFilters property of the Pipeline class. To filter any message, call the ProcessInputMessage method, which applies each filter in the collection to the incoming SOAP envelope. As each filter is applied, the ProcessMessage method is called to modify the SOAP envelope.

The following procedure explains how to add a custom input filter to an XML Web application. The procedure consists of two steps:

  • Creating the custom input filter.
  • Adding the custom input filter to the Web application.

To create a custom input filter

  1. Create a class library project in Visual Studio .NET 2003 called CustomFilterLibrary.

    1. Start Visual Studio .NET 2003.
    2. On the File menu, point to New, and then click Project.
    3. In the Project Types pane, select Visual C# Projects.
    4. In the Templates pane, select Class Library.
    5. In the Name box, enter the library name: CustomFilterLibrary
    6. Click OK.
  2. Add references to the Microsoft.Web.Services2 assembly.

    1. In Solution Explorer, right-click References, and then click Add Reference.
    2. Click the .NET tab, select Microsoft.Web.Services2.dll, and then click Select.
    3. Click OK.
  3. Add Imports or using directives to the top of the file that implements the custom output filter.

    1. In Solution Explorer, right-click the file containing the client code, and then click View Code.

    2. At the top of the file, add the Imports or using directives as shown in the following code example.

      Imports System
      Imports System.Xml
      Imports Microsoft.Web.Services2
      Imports System.Collections
      
      using System;
      using System.Xml;
      using Microsoft.Web.Services2;
      using System.Collections;
      
  4. Create a class that derives from the SoapInputFilter class and overrides the ProcessMessage method. Place the class in its own namespace, and implement the method to process and remove the custom header.

    Namespace CustomFilterLibrary
        ' Class derives from the SoapInputFilter class.
        Public Class CustomerGroupInputFilter
          Inherits SoapInputFilter
    
            ' This is an empty constructor.
            Public Sub New()
            End Sub 'New
    
    
            ' Override the ProcessMessage method.
            Public Overrides Sub ProcessMessage(envelope As SoapEnvelope)
               If Nothing = envelope Then
                   Throw New ArgumentNullException("No envelope!")
               End If 
               If Nothing <> envelope.Header Then
                   Dim nodeList As XmlNodeList = _
                   envelope.Header.GetElementsByTagName("CustomerGroup", _
                   "http://cohowinery.com/CustomFilterSample")
    
                   Dim removeList As New ArrayList()
    
                   If nodeList.Count > 1 Then
                        Throw New Exception( _
                        "Only one CustomerGroupId header is allowed.")
                   End If
    
                   Dim node As XmlNode
                   For Each node In  nodeList
                       If TypeOf node Is XmlElement Then
                           ' Only process the header if it is meant for
                           ' this node.
                           If CanProcessHeader(CType(node,XmlElement)) Then 
                               ' Process the header.
                               envelope.Context("CustomerGroupId") = _
                                 node.FirstChild.InnerText
    
                               ' Remember the header to remove it.
                               removeList.Add(node)
                           End If
                       End If
                   Next node
                   ' Remove headers. 
                   Dim node As XmlNode
                   For Each node In  removeList
                       envelope.Header.RemoveChild(node)
                   Next node
               End If
           End Sub 
        End Class
    End Namespace
    
    namespace CustomFilterLibrary
    {
        // Class derives from the SoapInputFilter class.
        public class CustomerGroupInputFilter : SoapInputFilter
        {
            public CustomerGroupInputFilter()
            { 
                // This is an empty constructor.
            }
    
            // Override the ProcessMessage method.
            public override void ProcessMessage(SoapEnvelope envelope)
            {
                if ( null == envelope )
                    throw new ArgumentNullException("No envelope!");
    
                if ( null != envelope.Header )
                {
                    XmlNodeList nodeList = 
                        envelope.Header.GetElementsByTagName(
                        "CustomerGroup",
                        "http://cohowinery.com/CustomFilterSample");
    
                    ArrayList removeList = new ArrayList();
    
                    if (nodeList.Count > 1)
                        throw new Exception(
                        "Only one CustomerGroupId header is allowed.");
    
                    foreach (XmlNode node in nodeList)
                    {
                        if ( node is XmlElement )
                        {
                        // Only process the header if it is meant for this
                        // node.
                            if (CanProcessHeader(node as XmlElement,
                                envelope.Context)) 
                            {
                                // Process the header.
                                envelope.Context["CustomerGroupId"] = 
                                node.FirstChild.InnerText;
    
                                // Remember the header to remove it.
                                removeList.Add(node);
                            }
                        }
                    }
                    // Remove headers. 
                    foreach ( XmlNode node in removeList )
                    {
                        envelope.Header.RemoveChild(node);
                    }
                }
            }
        }
    }
    

To add a custom input filter to a Web application

  • Open the Web service client or Web service project in Visual Studio .NET 2003 and register the output filter for the project.

    1. In Solution Explorer, right-click the file containing the application's configuration file.

    2. Add a <section> Element (WSE for Microsoft .NET) element to the <configuration> section for the Web.config file that affects the Web service.
      This adds the microsoft.web.services2 configuration section handler for this configuration file. The following code example shows how to add the microsoft.web.services2 configuration section handler.

      <configuration>
         <configSections>
            <section name="microsoft.web.services2"
                     type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
         </configSections>
      </configuration>
      
    3. In the configuration file, add the following XML under the <configuration> element, where CustomFilterLibrary is the namespace of your custom input filter and the library's assembly name, and CustomerGroupInputFilter is the name of the custom input filter class.

      <microsoft.web.services2>
          <filters>
              <input>
                  <add 
                     type="CustomFilterLibrary.CustomerGroupInputFilter, 
                     CustomFilterLibrary" />
              </input>
          </filters>
      </microsoft.web.services2>
      

See Also

Tasks

How to: Create and Add a Custom Output Filter

Other Resources

Filtering SOAP Messages