title: WCF Routing with custom filter: No matching MessageFilter was found for the given message
body: <p>Im writing a routing service for some WCF services. I want to use a custom message filter that filters out my message.</p>
  
  <p>Here is my custom message filter:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ServiceModel.Dispatcher;
  using System.IO;
  using System.Xml;
  
  namespace AMA_ISE.Router.MessageFilter
  {
      /// &lt;summary&gt;
      ///     This message filter implements the custom filtering for the routing services to re-route the incoming messages
      ///     correctly.
      /// &lt;/summary&gt;
      public class CustomMessageFilter : System.ServiceModel.Dispatcher.MessageFilter
      {
          /// &lt;summary&gt;
          ///     The sales channel for which the filter is configured.
          /// &lt;/summary&gt;
          private string _salesChannel;
  
          /// &lt;summary&gt;
          ///     The service, for which the filter is configured.
          /// &lt;/summary&gt;
          private string _service;
  
          /// &lt;summary&gt;
          /// Initializes a new instance of the &lt;see cref="CustomMessageFilter"/&gt; class.
          /// &lt;/summary&gt;
          /// &lt;param name="messageData"&gt;The message data.&lt;/param&gt;
          public CustomMessageFilter(string messageData)
          {
              var splitted = messageData.Split("|".ToCharArray());
              _salesChannel = splitted[0];
              _service = splitted[1];
          }
  
          /// &lt;summary&gt;
          /// When overridden in a derived class, tests whether a message satisfies the filter criteria. The body cannot be examined.
          /// &lt;/summary&gt;
          /// &lt;param name="message"&gt;The &lt;see cref="T:System.ServiceModel.Channels.Message"/&gt; object to test.&lt;/param&gt;
          /// &lt;returns&gt;
          /// true if the &lt;see cref="T:System.ServiceModel.Channels.Message"/&gt; object satisfies the filter criteria; otherwise, false.
          /// &lt;/returns&gt;
          public override bool Match(System.ServiceModel.Channels.Message message)
          {
              return true;
          }
  
          /// &lt;summary&gt;
          /// When overridden in a derived class, tests whether a buffered message satisfies the criteria of a filter.
          /// &lt;/summary&gt;
          /// &lt;param name="buffer"&gt;The &lt;see cref="T:System.ServiceModel.Channels.MessageBuffer"/&gt; object to test.&lt;/param&gt;
          /// &lt;returns&gt;
          /// true if the &lt;see cref="T:System.ServiceModel.Channels.MessageBuffer"/&gt; object satisfies the filter criteria; otherwise, false.
          /// &lt;/returns&gt;
          public override bool Match(System.ServiceModel.Channels.MessageBuffer buffer)
          {
              using (var memStream = new MemoryStream())
              {
                  var msgText = GetMessageEnvelope(buffer);
                  // check sales channel
                  if (GetValueByTagName(msgText, "SalesChannel") != _salesChannel)
                      return false;
                  // check the requested service
                  var toValue = GetValueByTagName(msgText, "To");
                  File.AppendAllText("C:\\temp\\filter.txt", toValue + " - " + _service + " - " + toValue.Trim().EndsWith(_service) + "\r\n");
                  return toValue.Trim().EndsWith(_service);
              }
          }
  
          /// &lt;summary&gt;
          /// Gets the message envelope.
          /// &lt;/summary&gt;
          /// &lt;param name="buffer"&gt;The buffer.&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          private static string GetMessageEnvelope(System.ServiceModel.Channels.MessageBuffer buffer)
          {
              using (MemoryStream memStream = new MemoryStream())
              {
                  var msg = buffer.CreateMessage();
                  XmlDictionaryWriter xmlDictWriter =
                      XmlDictionaryWriter.CreateTextWriter(memStream);
                  msg.WriteMessage(xmlDictWriter);
                  xmlDictWriter.Flush();
                  memStream.Seek(0, SeekOrigin.Begin);
                  var reader = new StreamReader(memStream);
                  var text = reader.ReadToEnd();
                  return text;
              }
          }
  
          /// &lt;summary&gt;
          /// Gets the name of the value by tag.
          /// &lt;/summary&gt;
          /// &lt;param name="text"&gt;The text.&lt;/param&gt;
          /// &lt;param name="tagName"&gt;Name of the tag which content we are searching for.&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          private static string GetValueByTagName(string text, string tagName)
          {
              var txt = text.Substring(text.IndexOf("&lt;" + tagName+" ") + 1);
              txt = txt.Substring(txt.IndexOf("&gt;") + 1);
              return txt.Substring(0, txt.IndexOf("&lt;"));
          }
      }
  }
  </code></pre>
  
  <p>As you can see, Im writing some information in a log file located in c:\temp. I expect to match the current filter when Match() returns true; that is not happening. When using this filter, I have the following text file:</p>
  
  <pre><code>http://localhost/AMA-ISE.Router/BookingService.svc - RetrieveService.svc - False
  http://localhost/AMA-ISE.Router/BookingService.svc - CancelService.svc - False
  http://localhost/AMA-ISE.Router/BookingService.svc - BookingService.svc - True
  http://localhost/AMA-ISE.Router/BookingService.svc - AvailService.svc - False
  http://localhost/AMA-ISE.Router/BookingService.svc - RebookingService.svc - False
  </code></pre>
  
  <p>That Looks nice, the BookingService.svc does fits and Match() will give true back. But it doesnt work. When I now use the routing service, I get the following error:</p>
  
  <p><strong>Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: No matching MessageFilter was found for the given Message.</strong> </p>
  
  <p>Whats wrong? Have I misunderstood something with the message filters? Please help me!</p>
  
  <p>Michael Baarz </p>