none
WCF - Envelope Namespace prefix using the web.config file RRS feed

  • Question

  • Hi All,

    There is an issue with the namespace prefix generation in the response in WCF.Please let me know if can use the web.config file with the below configuration for the same.

       <routing>      
          <namespaceTable>        
            <add namespace="http://www.afmkt.org/IntPtOd" prefix="order"></add>
          </namespaceTable>
        </routing>

    Please let me know how to proceed with this approach

    Tuesday, November 18, 2014 7:32 AM

Answers

  • Hello sm_1234,

    The following code snippet is taken from a sample framework but it illustrates how you could overwrite the contents of a message before it is sent using a message inspector.  As you want to modify the server's response you will need to create a IDispatchMessageInspector.

    I strongly advise you to try to find another way of handling this.  I have spent a very long time in integration and this is something that I have only had to a few times.  The only time I had to do this was when communicating to a legacy service (no longer maintained) or to a larger organization (like a bank) that chose not adhere to a standard.  I would question why the client is not complying to SOAP and explore making the client compliant.  

    If you do want to go down this route, then here is a code snippet to illustrate how it can be done.  This shows how the message can be completely replaced with a string.

    object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message message, System.ServiceModel.IClientChannel channel)
            {
                WebContentFormat messageFormat = this.GetMessageContentFormat(message);
                MemoryStream ms = new MemoryStream();
                XmlDictionaryWriter writer = null;
                switch (messageFormat)
                {
                    case WebContentFormat.Default:
                    case WebContentFormat.Xml:
                        writer = XmlDictionaryWriter.CreateTextWriter(ms);
                        break;
                    case WebContentFormat.Json:
                        writer = JsonReaderWriterFactory.CreateJsonWriter(ms);
                        break;
                    case WebContentFormat.Raw:
                        // special case for raw, easier implemented separately 
                        return this.ReadRawBody(ref message);
                }
    
                message.WriteMessage(writer);
                writer.Flush();
                string messageBody = Encoding.UTF8.GetString(ms.ToArray());
    
                // Here would be a good place to change the message body, if so desired. 
    
                // hack to illustrate to sm_1234 that the contents can be modified is desired
                messageBody = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><QuoteResponse xmlns=\"http://www.aftermarket.org/InternetPartsOrder\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"></QuoteResponse></s:Body></s:Envelope>";
                
                // now that the message was read, it needs to be recreated. 
                ms.Position = 0;
                
                ms = new MemoryStream(Encoding.UTF8.GetBytes(messageBody)); 
                XmlDictionaryReader reader;
                if (messageFormat == WebContentFormat.Json)
                {
                    reader = JsonReaderWriterFactory.CreateJsonReader(ms, XmlDictionaryReaderQuotas.Max);
                }
                else
                {
                    reader = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
                }
    
                Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
                newMessage.Properties.CopyProperties(message.Properties);
                message = newMessage;
    
                return messageBody;                      
            }


    Jeff

    Thursday, November 20, 2014 7:14 PM

All replies

  • Hello sm_1234,

    Is this related to your other query?

    I do not fully understand what you want to achieve.  Is the issue that the client is not sending soap messages that the server understands and you believe it is because the construction of the message is not correct?

    Is this a WCF service?  If so you might be able to solve this by modifying your proxy class.  If it is another type of soap parser (re., java) then it might be a bit trickier.

    If you just want to retrieve a namespace from configuration and insert that into your messages, then take a look at some of these posts:

    WCF Soap Envelope

    How to intercept raw messages

    Service Framework Project


    Jeff

    Tuesday, November 18, 2014 9:41 PM
  • Hi,

    For this scenario, in content-based routing, based upon the parameter(s) in the SOAP message the message is routed to a particular service endpoint. WCF comes with a set of built-in message filters that helps to filter the incoming SOAP messages based upon the SOAP action or endpoint or even through a particular parameter in the SOAP header or body. Some of the built-in message filters are MatchAllMessageFilter, ActionMessageFilter, XPathMessageFilter etc.

    For more detailed information, you could refer to:

    http://www.prideparrot.com/blog/archive/2011/8/routing_service_and_content_based_routing

    Besides, for customize WCF Envelope and Namespace Prefix:

    http://vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix

    http://blogs.msdn.com/b/carlosfigueira/archive/2010/06/13/changing-prefixes-in-xml-responses.aspx

    Regards

    Wednesday, November 19, 2014 2:49 AM
    Moderator
  • Thank you Jeff and Shawn.

    You are right Jeff!!

    The issue is the same of the prefix/attributes. The soap envelope  does not generate properly with message inspectors or message formatter.The below issue still persists.

    <order:QuoteResponse xmlns:order="http://www.aftermarket.org/InternetPartsOrder">
             <order:QuoteResult xsi:type="order:IPOQuoteAddQuoteResponse">

    <QuoteResponse xmlns="http://www.aftermarket.org/InternetPartsOrder" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
             <QuoteResult i:type="IPOQuoteAddQuoteResponse">

    Struggling since a very long time now :( Kindly let me know in case there is an approach for this issue.

    Thursday, November 20, 2014 10:09 AM
  • Hello sm_1234,

    The following code snippet is taken from a sample framework but it illustrates how you could overwrite the contents of a message before it is sent using a message inspector.  As you want to modify the server's response you will need to create a IDispatchMessageInspector.

    I strongly advise you to try to find another way of handling this.  I have spent a very long time in integration and this is something that I have only had to a few times.  The only time I had to do this was when communicating to a legacy service (no longer maintained) or to a larger organization (like a bank) that chose not adhere to a standard.  I would question why the client is not complying to SOAP and explore making the client compliant.  

    If you do want to go down this route, then here is a code snippet to illustrate how it can be done.  This shows how the message can be completely replaced with a string.

    object IClientMessageInspector.BeforeSendRequest(ref System.ServiceModel.Channels.Message message, System.ServiceModel.IClientChannel channel)
            {
                WebContentFormat messageFormat = this.GetMessageContentFormat(message);
                MemoryStream ms = new MemoryStream();
                XmlDictionaryWriter writer = null;
                switch (messageFormat)
                {
                    case WebContentFormat.Default:
                    case WebContentFormat.Xml:
                        writer = XmlDictionaryWriter.CreateTextWriter(ms);
                        break;
                    case WebContentFormat.Json:
                        writer = JsonReaderWriterFactory.CreateJsonWriter(ms);
                        break;
                    case WebContentFormat.Raw:
                        // special case for raw, easier implemented separately 
                        return this.ReadRawBody(ref message);
                }
    
                message.WriteMessage(writer);
                writer.Flush();
                string messageBody = Encoding.UTF8.GetString(ms.ToArray());
    
                // Here would be a good place to change the message body, if so desired. 
    
                // hack to illustrate to sm_1234 that the contents can be modified is desired
                messageBody = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><QuoteResponse xmlns=\"http://www.aftermarket.org/InternetPartsOrder\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"></QuoteResponse></s:Body></s:Envelope>";
                
                // now that the message was read, it needs to be recreated. 
                ms.Position = 0;
                
                ms = new MemoryStream(Encoding.UTF8.GetBytes(messageBody)); 
                XmlDictionaryReader reader;
                if (messageFormat == WebContentFormat.Json)
                {
                    reader = JsonReaderWriterFactory.CreateJsonReader(ms, XmlDictionaryReaderQuotas.Max);
                }
                else
                {
                    reader = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
                }
    
                Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
                newMessage.Properties.CopyProperties(message.Properties);
                message = newMessage;
    
                return messageBody;                      
            }


    Jeff

    Thursday, November 20, 2014 7:14 PM