如何修改WCF Soap消息体响应

时间:2017-06-15 17:08:02

标签: c# xml web-services wcf soap

我有以下接受和SOAP消息的WCF服务,我希望它返回SOAP消息响应。

    [ServiceContract]
    public interface IMyService
    {          
       [OperationContract(Action = "HotelAvailRQ", ReplyAction = "HotelAvailRQResponse")]
       [WebInvoke(Method = "POST",
       BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml)]
       Message HotelAvailRQ(Message message);       
    }

    public class MyService : IMyService
    {
       public Message HotelAvailRQ(Message message)
       {
           return ProcessMessage<OTA_HotelAvailRQ>(message);
       }

       public Message ProcessMessage<T>(Message message)
       {
           MessageBuffer buffer = message.CreateBufferedCopy(8192);

           // Get a copy of the original message. This will be used to read and extract the body.
           Message msgCopy = buffer.CreateMessage();

           // Take another copy of the same message. This will be used to return to the service. Returning an identical message forms part of the acknowledgement in this case.
           Message returnMsg = buffer.CreateMessage();

           XElement body = XElement.Parse(msgCopy.GetReaderAtBodyContents().ReadOuterXml());
           var instance = Deserialize<T>(body, "http://www.opentravel.org/OTA/2003/05");

           MethodInfo methodInfo = typeof(T).GetMethod("Process");
           object document = methodInfo.Invoke(instance, null);

           return returnMsg;

       }

       private static T Deserialize<T>(XElement xElement, string nameSpace)
       {
           using (MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xElement.ToString())))
           {
               XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), nameSpace);
               return (T)xmlSerializer.Deserialize(memoryStream);
           }
       }

       private static string Serialize(object obj)
       {
           using (MemoryStream memoryStream = new MemoryStream())
           {
               XmlSerializer xs = new XmlSerializer(obj.GetType());
               xs.Serialize(memoryStream, obj);
               return ASCIIEncoding.ASCII.GetString(memoryStream.ToArray());
           }
       }
   }  

   public partial class OTA_HotelAvailRQ
   {
       public static object Process()
       {
           try
           {
               OTA_HotelAvailRQ hotelAvailRQ = new OTA_HotelAvailRQ();
               //Do some stuff and return back new object
               return hotelAvailRQ ;
           }
           catch(Exception ex)
           {            
               return new OTA_HotelAvailRS(); //TODO Should be fault exception...
           }
       }
   }

我是否需要使用Body响应修改传入的SOAP消息?如果是,如何修改响应或创建新的SOAP消息响应?

更新:

我尝试了@popo的建议,这是

   var respObj = new object(); //resposneObjBody
   var settings = new XmlReaderSettings
   {
       IgnoreWhitespace = true
   };

   using (MemoryStream ms = new MemoryStream())
   {
       XmlSerializer xs = new XmlSerializer(respObj.GetType());
       xs.Serialize(ms, respObj);
       ms.Position = 0; '<< Added
       var reader = XmlReader.Create(ms, settings);
       var newMessage = Message.CreateMessage(reader, int.MaxValue, msgCopy.Version);
       newMessage.Headers.Clear();
       newMessage.Headers.CopyHeadersFrom(msgCopy.Headers);
   }

但必须添加ms.Position = 0;,因为我收到异常Root element is missing

但是,我现在遇到的问题是错误异常Unrecognized message version.我哪里错了?

1 个答案:

答案 0 :(得分:1)

不确定这是否有用,但如果不是,我会删除答案,也许其他人会有更具启发性的东西。这是我在消息检查器中使用的一些代码的修改版本,用于操作消息并将其传递给操作。 (以下未经测试):

    var respObj = new object(); //resposneObjBody you have to define this object type added just for filler
    var settings = new XmlReaderSettings
    {
        IgnoreWhitespace = true
    };

    using (MemoryStream ms = new MemoryStream())
    {
            XmlSerializer xs = new XmlSerializer(respObj.GetType());
            xs.Serialize(ms, respObj);
            ms.Position = 0; 
            var reader = XmlReader.Create(ms, settings);
            var newMessage = Message.CreateMessage(msgCopy.Version, null, reader); // action is null, but you may want to put your reply action here
            newMessage.Headers.Clear(); //you may not need this either
            newMessage.Headers.CopyHeadersFrom(msgCopy.Headers); //you may not need this either
            newMessage.Properties.CopyProperties(msgCopy.Properties); //optional??
    }