我有一个与 web 服务通信的客户端。我与之通信的类是通过 wsdl.exe 生成的 C# 类。我现在想记录所有传入和传出消息。
到目前为止,我所做的是编写一个从自动生成的 C # Cl 继承的类,并且我已经重写了 GetReaderForMessage 方法。这样我可以或多或少地访问传入的消息:
protected override XmlReader GetReaderForMessage(SoapMessage 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 方法做同样的事情。
我已经阅读了一些文章,建议我应该在这里使用 SoapExtensions,但从我可以理解的是,只有当我创建的“客户端”本身是一个 Web 服务,在这种情况下它不是。
有什么建议吗?
您需要使用“添加服务引用”而不是“添加 Web 引用”功能来使用此解决方案,如果服务是 ASMX 或 WCF,则可以使用它。(您需要使用.NET Framework 3.X 来使用此功能)
This article将帮助您将服务引用添加到 C# 项目中。
要截取请求和响应的 XML,请实现这两个类:
public cl InspectorBehavior : IEndpointBehavior
{
public string LastRequestXML {
get
{
return myMessageInspector.LastRequestXML;
}
}
public string LastResponseXML {
get
{
return myMessageInspector.LastResponseXML;
}
}
private MyMessageInspector myMessageInspector = new MyMessageInspector();
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void ApplyBehavior(ServiceEndpoint endpoint, Runtime clientRuntime)
{
clientRuntime.MessageInspectors.Add(myMessageInspector );
}
}
public cl MyMessageInspector : IMessageInspector
{
public string LastRequestXML { get; private set; }
public string LastResponseXML { get; private set; }
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
LastResponseXML = reply.ToString();
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IChannel channel)
{
LastRequestXML = request.ToString();
return request;
}
}
然后,将调用代码更改为:
MyTestServiceSoap client = new MyTestServiceSoap();
var requestInterceptor = new InspectorBehavior();
client.Endpoint.Behaviors.Add(requestInterceptor );
client.DoSomething("param1", "param2", "param3");
string requestXML = requestInterceptor.LastRequestXML;
string responseXML = requestInterceptor.LastResponseXML;
* * * * EDIT * * * * 这与服务器端技术无关,您可以将其与 WCF,ASMX,PHP,...Web 服务一起使用,我已经在:http://www.w3schools.com/webservices/tempconvert.asmx上进行了测试
并得到了以下 XML:
requestXML =
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/CelsiusToFahrenheit</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CelsiusToFahrenheit xmlns="http://tempuri.org/">
<Celsius>50</Celsius>
</CelsiusToFahrenheit>
</s:Body>
</s:Envelope>
responseXML =
<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">
<s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
<soap:Body>
<CelsiusToFahrenheitResponse xmlns="http://tempuri.org/">
<CelsiusToFahrenheitResult>122</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse>
</soap:Body>
</soap:Envelope>
* * * * 编辑 2 * * * *
“添加 Web 引用”不是专门针对 ASMX 的,也不是 ASMX 客户端技术,“添加服务引用”也不是 WCF 客户端技术,您可以使用两者来添加对 ASMX,WCF,JSP 开发或 PHP 开发的 Web 服务的引用,您需要您的应用程序使用.Net 框架 3.5 来使用“添加服务引用”。
This article提到:
在 Visual Studio 中使用“添加 Web 引用”对话框时,将使用 WSDL 信息生成客户端代理并将其添加到 Visual Studio 项目中。这通常用于 ASMX 服务,但您也可以使用“添加 Web 引用”对话框为 WCF 服务创建客户端代理。但是,您需要手动键入服务 URL,并且生成的代理使用 XML 序列化程序.exe,这是您支持的唯一序列化类型的 WCF 服务。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(49条)