none
WCF Change Soap Message Body in IDispatchMessageInspector BeforeReply Method RRS feed

  • Question

  • Hi I have a operation  and for my everyoperation I have a ResponseInterface ,What I want for everycondition like soap fault,I want to return my operation response.I try to use createmessage and change it with original message.But that time because of soap message difference in client I got serialization error.How can I solve this

            public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
            {

    System.ServiceModel.Channels.Message m = System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Soap12, "urn: MyAction", myresponseobject)

    .....

    reply=m; 

    }

    but when I check soap created soap message it is different my original message.Normally I use myresponseobject both succesfull and failed message but occuring soap message is different.

    • I have a sample service.and if Successfull message my soap body something like that

    <s:Body>
      <MyCountryListRequestResponse xmlns="http://mysite.com"> 
      <MyCountryListRequestResult xmlns:d4p1="http://schemas.mysite.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">    
      <d4p1:Body>   <d4p1:CountryList>            <d4p1:CountryInfo>....

    <d4p1:Header xmlns:d5p1="http://schemas.mysite.com">          <d5p1:Message> ..

    •     And if some unexpected soap error occurs ,by default soap message something like that
    •  <s:Body>
    •     <s:Fault>
            <s:Code>
              <s:Value>s:Receiver</s:Value>
              <s:Subcode>
                <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>
              </s:Subcode>
            </s:Code>
            <s:Reason>
              <s:Text xml:lang="tr-TR">unexpected error information ...</s:Text>
            </s:Reason>
            <s:Detail>
              <ExceptionDetail xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel">
                <HelpLink i:nil="true" />
                <InnerException i:nil="true" />
                <Message>Consumer user cant have authenticate right in target pool</Message>
                <StackTrace>

    What I want to customize message,with creating message,with serializing my entity and return to my client like that,but this message is different my original soap message,there are extra tag in soap like ...Result.What I want to achive in unexpected error case,I can return my entity anyway

      <s:Body>
        <MyCountryListResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.mysite.com">
          <Body i:nil="true" />
          <Header xmlns:d4p1="http://schemas.mysite.com">
            <d4p1:Message>unexpected error information ...
    Tuesday, January 9, 2018 6:47 AM

All replies

  • Hi Bilgehan,

    >> I have a ResponseInterface ,What I want for everycondition like soap fault,I want to return my operation response.

    Are the ResponseInterface and “myresponseobject” the same interface? If not, I think it is expected that you got a deserialization error at client side.

    For returning the same myresponseobject for successful and fail, I think the WCF Method should return the same myresponseobject structure. You could not return Fault Exception, or you should recreate the reply message by myresponseobject.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 10, 2018 5:10 AM
  • Hi Tao ResponseInterface and “myresponseobject are same.

    I create  reply message with myresponseobject (You could not return Fault Exception, or you should recreate the reply message by myresponseobject.)

    And I return my new instance but  new soap message format is different from orginial ,you can see above 

    This original created successfullmymessage return myresponseobject type

    <s:Body>
      <MyCountryListRequestResponse xmlns="http://mysite.com"> 
      <MyCountryListRequestResult xmlns:d4p1="http://schemas.mysite.com" (This message created with operationdispatcher.Invoker with myresponseobject)

    But when I want to create instance of my object that time soap message is different.What I am missing,Original soap message I see some extra tag ..............Result,(you can see above).

    (This message created IDispatchMessageInspector )

    System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Soap12, "urn: MyAction", myresponseobject)

     <s:Body>
        <MyCountryListResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.mysite.com">
       


    Wednesday, January 10, 2018 6:09 AM
  • Hi Bilgehan,

    If you replace the different part before creating the new message, something like below:

    public void XMLWriterTEst2()
            {
                var outputtype = typeof(MyResponse);
                var outputInstance = new MyResponse() { Success = false, Info = "UnexpectedError occured" };
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.ConformanceLevel = ConformanceLevel.Auto;
                settings.CloseOutput = true;
                using (var sww = new StringWriter())
                {
                    using (XmlWriter writer = XmlWriter.Create(sww, settings))
                    {
                        writer.WriteStartElement("root");
                        XmlSerializer serializer = new XmlSerializer(outputtype);
                        serializer.Serialize(writer, outputInstance);
                        writer.WriteEndElement();
                        writer.Flush();
                        string xml2 = sww.ToString(); 
                        //modify the xml string like thr successfully response
                        Encoding encoding = Encoding.UTF8;
                        MemoryStream ms = new MemoryStream(encoding.GetBytes(xml2));
                        XmlReader bodyReader = XmlReader.Create(ms);
    
                        System.ServiceModel.Channels.Message m = System.ServiceModel.Channels.Message.CreateMessage(MessageVersion.Soap12, "urn: MyAction", bodyReader);
                        //show message stream body
                        MessageBuffer messageBuffer = m.CreateBufferedCopy(int.MaxValue);
                        System.ServiceModel.Channels.Message message = messageBuffer.CreateMessage();                  
    
                    }
                }
            }

    Will you get any error?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 10, 2018 6:48 AM
  • This is not work because at the end new message is different from original soap message,giving return type of operation is not enough.I try to explain a scenario

    I have a success call-->Creating soap like that

    <s:Body>
      <MyCountryListRequestResponse xmlns="http://mysite.com"> 
      <MyCountryListRequestResult xmlns:d4p1="http://schemas.mysite.com" (This message created with operationdispatcher.Invoker with myresponseobject)

    I have soap fail call-->Normal Message like that

    •  <s:Body>
    •     <s:Fault>
            <s:Code>
              <s:Value>s:Receiver</s:Value>
              <s:Subcode>
                <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</s:Value>

    when I try to change this fault call with creating new message(serializing outputtype of operation)

    new message like that

    <s:Body>
        <MyCountryListResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.mysite.com">

    As result return soap message pattern is not match


    Wednesday, January 10, 2018 8:01 AM
  • Hi Bilgehan,

    Could you share us a simple project which could reproduce your issue through OneDrive or Github?

    I think a reproduceable project would be easy to understand your issue.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 10, 2018 8:11 AM
  • Hello, 

    Did you find a solution for your problem? cuz I'm kind stuck with the same situation 


    Monday, August 27, 2018 9:09 AM