xh_green
2017-05-16 09:41
采纳率: 100%
浏览 989
已采纳

Web Service中在 SOAP扩展类 中处理Request/Response

(C#/.net 新手)在做一Web Service, 过程是用SOAP协议,我要做的是解析 Client 端发过来的 POST/GET的 XML流, 完了产生一个XML流返回给Client

查了下,需要用到SOAP扩展重写SoapExtension和SoapExtensionAttribute去做,但是有个问题是目前不太了解重新建立的XML流如何交给返回的数据流

public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Output(message);
break;
case SoapMessageStage.BeforeDeserialize:
Input(message);
break;
case SoapMessageStage.AfterDeserialize:
break;
default:
throw new Exception("invalid stage");
}
}


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • xh_green 2017-05-18 06:47
    已采纳

    方法:
    在序列化之后进行xml的改写

     public void WriteOutput(SoapMessage message)// AfterSerialize后,服务器返回xml数据,_oldStream为空,改写_newStream
            {
                FileStream fs;
                StreamWriter w = null; 
    
                try
                {
                    if (_logTypeMode.Equals(LogType.RequestOnly) || _logTypeMode.Equals(LogType.RequestReponse))
                    {
                        _newStream.Position = 0;
                        // 改掉_newStream的内容
                        fs = new FileStream(_reqFilename, FileMode.Append, FileAccess.Write);
                        w = new StreamWriter(fs);
                        try { 
                            StringBuilder sb = new StringBuilder();
                            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                            sb.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
                            sb.Append("<soap:Body>");
                            sb.Append("<HelloWorldResponse xmlns=\"http://tempuri.org/\">");
                            sb.Append("<HelloWorldResult>Hello World Service 1234567 - ````````222222222</HelloWorldResult>");
                            sb.Append("</HelloWorldResponse>");
                            sb.Append("</soap:Body>");
                            sb.Append("</soap:Envelope>");
    
                            byte[] strBytes = Encoding.ASCII.GetBytes(sb.ToString());
    
                            Copy(new MemoryStream(strBytes), _newStream);
                        }catch(Exception ex){
                            w.WriteLine("Error Writting");
                            w.Flush();
                        }
    
    
                        _newStream.Position = 0;
                        string soapString = "Server-SoapResponse";
                        w.WriteLine("-----" + soapString + " at 11111xOutput _newStream" + DateTime.Now);
                        w.Flush();
                        Copy(_newStream, fs);
    
                        w.Close();
    
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (w != null) w.Close();
                    _newStream.Position = 0;
                    Copy(_newStream, _oldStream);
    
                }
            }
    
    打赏 评论

相关推荐 更多相似问题