. net SoapExtension不能修改服务器端的soap请求



我正在尝试在。net中实现一个SoapExtension,它将压缩所有soap流量(请求和响应)。我可以控制客户端和服务器。我已经开始复制这个:http://www.mastercsharp.com/article.aspx?ArticleID=86&&TopicID=7并稍微修改它,我有一个问题。

当它从客户端发送时,我能够修改(zip)请求体,它到达服务器,反向操作(解压缩)也很好,但我似乎无法得到框架来反映我对流对象所做的更改!在服务器上,我将正确的数据设置为newStream对象,但是当调用WebMethod时,参数仍然被压缩(因此无效)。知道为什么吗?

为了让包括我自己在内的每个人都更容易,我将SoapExtension简化为:

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Net;
// Define a SOAP Extension that traces the SOAP request and SOAP
// response for the XML Web service method the SOAP extension is
// applied to.
public class TraceExtension : SoapExtension {
Stream oldStream;
Stream newStream;
string filename;
// Save the Stream representing the SOAP request or SOAP response into
// a local memory buffer.
public override Stream ChainStream(Stream stream)
{
    oldStream = stream;
    newStream = new MemoryStream();
    return newStream;
}
// When the SOAP extension is accessed for the first time, the XML Web
// service method it is applied to is accessed to store the file
// name passed in, using the corresponding SoapExtensionAttribute.  
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
    return ((TraceExtensionAttribute)attribute).Filename;
}
// The SOAP extension was configured to run using a configuration file
// instead of an attribute applied to a specific XML Web service
// method.
public override object GetInitializer(Type WebServiceType)
{
    // Return a file name to log the trace information to, based on the
    // type.
    return "C:\" + WebServiceType.FullName + ".log";
}
// Receive the file name stored by GetInitializer and store it in a
// member variable for this specific instance.
public override void Initialize(object initializer)
{
    filename = (string)initializer;
}
//  If the SoapMessageStage is such that the SoapRequest or
//  SoapResponse is still in the SOAP format to be sent or received,
//  save it out to a file.
public override void ProcessMessage(SoapMessage message)
{
    switch (message.Stage)
    {
        case SoapMessageStage.BeforeSerialize:
            break;
        case SoapMessageStage.AfterSerialize:
            WriteOutput(message);
            break;
        case SoapMessageStage.BeforeDeserialize:
            WriteInput(message);
            break;
        case SoapMessageStage.AfterDeserialize:
            break;
    }
}
public void WriteOutput(SoapMessage message)
{
    newStream.Position = 0;
    FileStream fs = new FileStream(filename, FileMode.Append,
        FileAccess.Write);
    StreamWriter w = new StreamWriter(fs);
    string soapString = (message is SoapServerMessage) ? "SoapResponse" : "SoapRequest";
    w.WriteLine("-----" + soapString + " at " + DateTime.Now);
    w.Flush();
    Copy(newStream, fs);
    w.Close();
    newStream.Position = 0;
    Copy(newStream, oldStream);
}
//public void WriteInput(SoapMessage message)
//{
//    Copy(oldStream, newStream);
//    FileStream fs = new FileStream(filename, FileMode.Append,
//        FileAccess.Write);
//    StreamWriter w = new StreamWriter(fs);
//    string soapString = (message is SoapServerMessage) ?
//        "SoapRequest" : "SoapResponse";
//    w.WriteLine("-----" + soapString +
//        " at " + DateTime.Now);
//    w.Flush();
//    newStream.Position = 0;
//    Copy(newStream, fs);
//    w.Close();
//    newStream.Position = 0;
//}

public void WriteInput(SoapMessage message)
{
    oldStream.Position = 0;
    StreamReader reader = new StreamReader(oldStream);
    StreamWriter writer = new StreamWriter(newStream);
    string data = reader.ReadToEnd();
    data = data.Replace("false", "true");
    writer.Write(data);
    writer.Flush();
}
void Copy(Stream from, Stream to)
{
    TextReader reader = new StreamReader(from);
    TextWriter writer = new StreamWriter(to);
    writer.WriteLine(reader.ReadToEnd());
    writer.Flush();
}

}

它仍然不能正常工作。当服务器接收到soap请求(WriteInput方法)时,所有的"false"值都被更改为"true",数据被保存到newStream对象中,但它仍然会在webmethod调用中显示"false"值!

我的调用是这样的:客户:

 [SoapDocumentMethodAttribute("http://Company.com/Product/admintool/webservices/SaveNativeRequest", RequestNamespace = "http://Company.com/Product/admintool/webservices/", ResponseNamespace = "http://Company.com/Product/admintool/webservices/", Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
        [SoapHeaderAttribute("Ticket")]
        [TraceExtension(Priority = 0)]
        public void SaveNativeRequest(XmlNode nativeRequest, int? batchId)
        {
            this.Invoke("SaveNativeRequest", new object[] { nativeRequest, batchId });
        }
服务器:

 [WebMethod]
    [TraceExtension(Priority = 0)]
    [SoapHeader("Ticket", Direction = SoapHeaderDirection.In)]
    public void SaveNativeRequest(XmlNode nativeRequest, int? batchId)
    {...}

为了完整,这是属性类

// Create a SoapExtensionAttribute for the SOAP Extension that can be
// applied to an XML Web service method.
using System;
using System.Web.Services.Protocols;
[AttributeUsage(AttributeTargets.Method)]
public class TraceExtensionAttribute : SoapExtensionAttribute
{
    private string filename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"log.txt";
    private int priority;
    public override Type ExtensionType
    {
        get { return typeof(TraceExtension); }
    }
    public override int Priority
    {
        get { return priority; }
        set { priority = value; }
    }
    public string Filename
    {
        get
        {
            return filename;
        }
        set
        {
            filename = value;
        }
    }
}

我已经看了这篇文章:h**p://hyperthink.net/blog/inside-of-chainstream/,我很确定我正确地使用了chainstream方法。我知道还有其他方法,但这似乎是最干净的选择。我读过关于这个主题的各种文章,这篇http://msdn.microsoft.com/en-us/magazine/cc188761.aspx文章启发了我一个解决方法,我可以使用

(System.Web.HttpContext.Current.Items["RequestSoapContext"] as Microsoft.Web.Services3.SoapContext).Envelope.Body

在ProcessMessage方法中直接操作soap体(这工作得很好,但是很难看)

我是否忽略了一些明显的东西?应用程序的其他部分是否会干扰服务器上的流链?如果有人对此有任何见解,我将非常非常高兴。

我认为你需要重置newStream在WriteInput方法中的位置。

所以请在WriteInput方法的末尾添加以下一行:

newStream.Position = 0;

应该能解决你的问题。

最新更新