在反序列化之前无法更改soap消息内容

时间:2015-12-25 13:50:57

标签: c# soap

我有asp.net网络服务和SoapExtension:

public class PrefixExtention : SoapExtension
{
    Stream _originalStream;
    Stream _workingStream;

    public override Stream ChainStream(Stream stream)
    {
        _originalStream = stream;

        _workingStream = new MemoryStream();
        return _workingStream;
    }

    public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    {
        return ((PrefixExtentionAttribute)attribute).Filename;
    }

    public override object GetInitializer(Type WebServiceType)
    {
        return null;
    }

    public override void Initialize(object initializer)
    {
        // do nothing
    }

    public override void ProcessMessage(SoapMessage message)
    {
        switch (message.Stage)
        {
            case SoapMessageStage.BeforeSerialize:
                break;
            case SoapMessageStage.AfterSerialize:
                break;
            case SoapMessageStage.BeforeDeserialize:
                Copy(_originalStream, _workingStream);
                ChangeStream(_workingStream);
                break;
            case SoapMessageStage.AfterDeserialize:
                Copy(_originalStream, _workingStream);
                LogMessageFromStream("1.txt", _workingStream);
                break;
            default:
                throw new Exception("invalid stage");
        }
    }

    void Copy(Stream from, Stream to)
    {
        TextReader reader = new StreamReader(from);
        TextWriter writer = new StreamWriter(to);
        writer.WriteLine(reader.ReadToEnd());
        writer.Flush();
    }

    private void LogMessageFromStream(string filename, Stream stream)
    {
        string soapMessage = string.Empty;

        if (stream.CanRead && stream.CanSeek)
        {
            stream.Position = 0;

            StreamReader rdr = new StreamReader(stream);
            soapMessage = rdr.ReadToEnd();
            stream.Position = 0;
        }

        File.WriteAllText(filename, soapMessage);
    }

    private void ChangeStream(Stream stream)
    {
        Copy(_originalStream, _workingStream);

        string soapMessage = string.Empty;

        if (stream.CanRead && stream.CanSeek)
        {
            stream.Position = 0;

            StreamReader rdr = new StreamReader(stream);
            soapMessage = rdr.ReadToEnd();

            stream.Position = 0;
        }

        // do something with message
        soapMessage = soapMessage.Replace("a:", "");
        soapMessage = soapMessage.Replace("xmlns:a=", "xmlns=");

        MemoryStream memoryStream = new MemoryStream();
        StreamWriter writer = new StreamWriter(memoryStream);
        writer.Write(soapMessage);
        writer.Flush();
        memoryStream.Position = 0;

        _workingStream.Position = 0;
        Copy(memoryStream, _workingStream);
        memoryStream.Close();
        _workingStream.Position = 0;
    }
}

在方法ChangeStream()中,我正在尝试更改邮件内容 - 删除前缀" a"来自xml。我通过Copy更改_workingStream(memoryStream,_workingStream);但是没有调用SoapMessageStage.AfterDeserialize。如何修改soap消息,以便使用修改后的消息正确反序列化我的对象(在Web方法中)?

1 个答案:

答案 0 :(得分:0)

我相信这是因为你正在处理字节而不是字符串,所以你首先必须将传递给Stream的{​​{1}}转换为字符串。有关详细信息,请参阅this answer

编辑:以上是错误的。您似乎已经在阅读流并对其进行编码。但是,如果tge stream为null,它看起来像是在尝试替换字符串对象中可能为null的字符。您确定从服务电话中获取数据吗?