在客户端拦截来自和接收到 Web 服务的 SOAP 消息 [英] Intercept SOAP messages from and to a web service at the client

查看:45
本文介绍了在客户端拦截来自和接收到 Web 服务的 SOAP 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与 Web 服务通信的客户端.我与之通信的类是通过 wsdl.exe 生成的 C# 类.我现在想记录所有传入和传出的消息.

I have a client that communicates with a web service. The class that I communicate with is a C# class that is generated through wsdl.exe. I now want to log all incoming and outgoing messages.

到目前为止我所做的是编写一个继承自自动生成的 C# 类的类,并且我已经覆盖了 GetReaderForMessage 方法.这样我就可以或多或少地像这样访问传入的消息:

What I've done so far is to write a class that inherits from the automatically generated C# Class and I have overridden the GetReaderForMessage method. That way I can access the incoming message more or less like this:

protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)
{
    System.Xml.XmlReader aReader = base.GetReaderForMessage(message, bufferSize);
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(aReader);
    string content = doc.InnerXml.ToString();
    System.Xml.XmlReader aReader2 = System.Xml.XmlReader.Create(new System.IO.StringReader(content));

    return aReader2;
}

显然我对这个解决方案不太满意,因为基本上我正在创建两个 xml 阅读器.一种用于读取 SOAP 消息的内容,另一种用于返回给方法调用者.另外,我不能用 GetWriterForMessage 方法做同样的事情.

Obviously I'm not too happy with this solution, because basically I'm creating two xml readers. One to read the contents of the SOAP message and one to return to the method caller. Plus I can't really do the same with the GetWriterForMessage method.

但可能是我做的事情太难开始了.例如,是否可以直接读取 SoapClientMessage 对象的内容?我读过一些建议我应该在这里使用 SoapExtensions 的文章,但据我所知,只有当我创建的客户端"本身是一个 Web 服务时才有效,而在这种情况下它不是.

But may be I'm just doing things too difficult to start with. Is it for instance possible to read the contents of the SoapClientMessage object directly? I've read some articles suggesting that I should use SoapExtensions here, but from what I can understand, that would only work if the 'client' that I am creating is itself a web service which in this case it is not.

有什么建议吗?

推荐答案

我建议考虑使用 SOAP 扩展,在我看来,它非常适合这种情况.以下是一些描述该过程的链接.

I would suggest looking into using a SOAP extension, which in my opinion is ideal for this scenario. Here are a few links that describe the process.

http://msdn.microsoft.com/en-us/magazine/cc164007.aspx

https://ebay.custhelp.com/cgi-bin/ebay.cfg/php/enduser/std_adp.php?p_faqid=350

http://www.codeproject.com/KB/webservices/efficientsoapextension.aspx

这篇关于在客户端拦截来自和接收到 Web 服务的 SOAP 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆