为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕尼黑的夜晚无繁华 2022-12-31 13:38:34
我想调用一个 Web 服务,我想获取请求和响应对象作为肥皂消息。var response = client.invoke(parameter);我想以某种方式发送消息并接收消息。
查看完整描述

1 回答

?
DIEA

TA贡献1820条经验 获得超3个赞

基于来自 Carlos Figueira 的 MSDN 文章WCF Extensibility – Message Inspectors,一种选择是使用 MessageInspector。

创建一个实现EndBehaviorClientMessageInspector的类。缓冲请求和回复消息,以便稍后使用它们。在这个例子中,我在控制台中打印它们。

这是组合的实现:

// using System.ServiceModel.Description;

// using System.ServiceModel.Dispatcher;

// using System.ServiceModel.Channels;

// using System.ServiceModel

public class InspectBehaviorAndnspector : IEndpointBehavior, IClientMessageInspector

{


    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)

    {

        clientRuntime.MessageInspectors.Add(this);

    }


    public MessageBuffer RequestBuffer;

    public MessageBuffer ReplyBuffer;


    public void AfterReceiveReply(ref Message reply, object correlationState){

       // messages are read only

       ReplyBuffer = reply.CreateBufferedCopy(2048);

       // so recreate the message after it was buffered

       reply = ReplyBuffer.CreateMessage();

    }


    public object BeforeSendRequest(ref Message request, IClientChannel channel){

       // messages are read only

       RequestBuffer = request.CreateBufferedCopy(2048);

       // so recreate the message after it was buffered

       request = RequestBuffer.CreateMessage();

       return "42";

    }


    // not needed for client

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)

    {

    }


    public void Validate(ServiceEndpoint sep) 

    {

    }

    public void AddBindingParameters(ServiceEndpoint sep, BindingParameterCollection bpc)

    {

    }

}

现在在您的client实例上,假设它源自ClientBase,您可以执行以下操作:


var inspector = new InspectBehaviorAndnspector();

client.Endpoint.Behaviors.Add(inspector);


// this is your call

var response = client.invoke(parameter);


// either do a ToString

Console.WriteLine(inspector.RequestBuffer.CreateMessage().ToString());

// or Write it with XmlWriter

var sb = new StringBuilder();

using(var xw = XmlWriter.Create(sb, new XmlWriterSettings {Indent =true})) {

    inspector.ReplyBuffer.CreateMessage().WriteMessage(xw);

}

Console.WriteLine(sb.ToString());

我在一个带有添加服务的例子中运行了这个,这是我的结果:


<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/ISelfHostTest/Add</Action>

  </s:Header>

  <s:Body>

    <Add xmlns="http://tempuri.org/">

      <x>3</x>

      <y>2</y>

    </Add>

  </s:Body>

</s:Envelope>


<?xml version="1.0" encoding="utf-16"?>

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Body>

    <AddResponse xmlns="http://tempuri.org/">

      <AddResult>5</AddResult>

    </AddResponse>

  </s:Body>

</s:Envelope>


查看完整回答
反对 回复 2022-12-31
  • 1 回答
  • 0 关注
  • 82 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号