在调用 Web 服务时,是否有一种简单的方法来获取请求的 soap 消息和响应的 soap 消息?

我想调用一个 Web 服务,我想获取请求和响应对象作为肥皂消息。

var response = client.invoke(parameter);

我想以某种方式发送消息并接收消息。


慕尼黑的夜晚无繁华
浏览 83回答 1
1回答

DIEA

基于来自 Carlos Figueira 的 MSDN 文章WCF Extensibility – Message Inspectors,一种选择是使用 MessageInspector。创建一个实现EndBehavior和ClientMessageInspector的类。缓冲请求和回复消息,以便稍后使用它们。在这个例子中,我在控制台中打印它们。这是组合的实现:// using System.ServiceModel.Description;// using System.ServiceModel.Dispatcher;// using System.ServiceModel.Channels;// using System.ServiceModelpublic class InspectBehaviorAndnspector : IEndpointBehavior, IClientMessageInspector{&nbsp; &nbsp; public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; clientRuntime.MessageInspectors.Add(this);&nbsp; &nbsp; }&nbsp; &nbsp; public MessageBuffer RequestBuffer;&nbsp; &nbsp; public MessageBuffer ReplyBuffer;&nbsp; &nbsp; public void AfterReceiveReply(ref Message reply, object correlationState){&nbsp; &nbsp; &nbsp; &nbsp;// messages are read only&nbsp; &nbsp; &nbsp; &nbsp;ReplyBuffer = reply.CreateBufferedCopy(2048);&nbsp; &nbsp; &nbsp; &nbsp;// so recreate the message after it was buffered&nbsp; &nbsp; &nbsp; &nbsp;reply = ReplyBuffer.CreateMessage();&nbsp; &nbsp; }&nbsp; &nbsp; public object BeforeSendRequest(ref Message request, IClientChannel channel){&nbsp; &nbsp; &nbsp; &nbsp;// messages are read only&nbsp; &nbsp; &nbsp; &nbsp;RequestBuffer = request.CreateBufferedCopy(2048);&nbsp; &nbsp; &nbsp; &nbsp;// so recreate the message after it was buffered&nbsp; &nbsp; &nbsp; &nbsp;request = RequestBuffer.CreateMessage();&nbsp; &nbsp; &nbsp; &nbsp;return "42";&nbsp; &nbsp; }&nbsp; &nbsp; // not needed for client&nbsp; &nbsp; public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public void Validate(ServiceEndpoint sep)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public void AddBindingParameters(ServiceEndpoint sep, BindingParameterCollection bpc)&nbsp; &nbsp; {&nbsp; &nbsp; }}现在在您的client实例上,假设它源自ClientBase,您可以执行以下操作:var inspector = new InspectBehaviorAndnspector();client.Endpoint.Behaviors.Add(inspector);// this is your callvar response = client.invoke(parameter);// either do a ToStringConsole.WriteLine(inspector.RequestBuffer.CreateMessage().ToString());// or Write it with XmlWritervar sb = new StringBuilder();using(var xw = XmlWriter.Create(sb, new XmlWriterSettings {Indent =true})) {&nbsp; &nbsp; inspector.ReplyBuffer.CreateMessage().WriteMessage(xw);}Console.WriteLine(sb.ToString());我在一个带有添加服务的例子中运行了这个,这是我的结果:<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">&nbsp; <s:Header>&nbsp; &nbsp; <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ISelfHostTest/Add</Action>&nbsp; </s:Header>&nbsp; <s:Body>&nbsp; &nbsp; <Add xmlns="http://tempuri.org/">&nbsp; &nbsp; &nbsp; <x>3</x>&nbsp; &nbsp; &nbsp; <y>2</y>&nbsp; &nbsp; </Add>&nbsp; </s:Body></s:Envelope><?xml version="1.0" encoding="utf-16"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">&nbsp; <s:Body>&nbsp; &nbsp; <AddResponse xmlns="http://tempuri.org/">&nbsp; &nbsp; &nbsp; <AddResult>5</AddResult>&nbsp; &nbsp; </AddResponse>&nbsp; </s:Body></s:Envelope>
打开App,查看更多内容
随时随地看视频慕课网APP