简体   繁体   中英

Error updating Wcf message before hitting service using IDispatchOperationSelector

We are having to intercept a SOAP message before it hits our WCF service to perform the following steps:

  • Route the message to the correct method as the client is unable to provide us with a SOAPAction value.
  • Update the namespaces of the xml as the client is unable to add namespace information to the message.

The routing is not an issue, but we are having a problem with creating the message; once we recreate the message the body merely consists of "... Stream ...".

Before creating the message, the messageContent variable contains valid, correct xml.

private Message UpdateNamespaces(Message message, string methodName)
    {
        var memoryStream = new MemoryStream();
        var xmlWriter = XmlWriter.Create(memoryStream);
        message.WriteMessage(xmlWriter);
        xmlWriter.Flush();

        var messageContent = Encoding.UTF8.GetString(memoryStream.ToArray());
        xmlWriter.Close();

        // Update messageContent with corrected XML

        memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(messageContent));
        var xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(memoryStream, new XmlDictionaryReaderQuotas());
        var newMessage = Message.CreateMessage(xmlDictionaryReader, int.MaxValue, message.Version);
        newMessage.Properties.CopyProperties(message.Properties);

        return newMessage;
    }

The messageContent is correct at the point at which we create the memoryStream, but as soon as I check the content of newMessage.ToString(), I'm getting the "... Stream ..." body content.

If anyone could help, I'd be very grateful as I'm out of ideas!

Many thanks

I think you are mixing behavior responsibilities.

IDispatchOperationSelector.SelectOperation is intended for routing decisions. It gives you a reference to the message, but the intention is to enable access to message properties. WCF does not expect the you to modify the message using this extension point. I can't say definitively, but the problem could be that simple.

If you want to alter the message namespace you should be using IDispatchMessageInspector . I suggest creating a second behavior for that task. Here's a nice Message Inspector example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM