C# 更换WCF生产的肥皂

C# 更换WCF生产的肥皂,c#,wcf,soap,C#,Wcf,Soap,请告诉我如何换一点肥皂 下面是发生的情况: <s: Envelope> <s: Header> ... </s: Header> <s: Body> <Method1 xmlns="http://my.site"> <Param1 xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> ... 我需要去 <s: Envelope

请告诉我如何换一点肥皂

下面是发生的情况:

<s: Envelope>
<s: Header>
...
</s: Header>
<s: Body>
<Method1 xmlns="http://my.site">                  
  <Param1 xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
...
我需要去

<s: Envelope>
<s: Header>
...
</s: Header>
<s: Body>
  <Param1 xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Value1>...
    <Value2>...
    ...

您可以查看接口IClientMessageInspector并从中派生

public class SOAPInspector : IClientMessageInspector {
public void AfterReceiveReply(ref Message reply, object correlationState) {
  //Your code
}

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel) {
    //Your code
}
}
使用BeforeSendRequest方法,可以在发送之前修改生成的SOAP消息

要运行检查器,您还需要一个EndpointBehavior类,如:

public class SOAPEndpointBehaviour : IEndpointBehavior {

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) {
      clientRuntime.MessageInspectors.Add(new SOAPInspector());
    }
}
最后记录下你的行为

yourServiceClient.Endpoint.EndpointBehaviors.Add(new SOAPEndpointBehaviour());

解决方案是使用MessageContract 但是试图用它解决问题-与另一个冲突:

这很有趣。我遇到的问题,在大多数描述MessageContract的文章中都有! 在所有这些使用嵌套复杂类型的代码中,到处都体现了这个工件!只要看一眼,就会发现它。但是,就本文而言,这个工件并不重要。因此,这是没有人关注的。
不幸的是,这个bug非常严重。

我们必须为此解析整个SOAP请求?@SoaperPlus恐怕是的。但是.NET为类似的东西提供了相当舒适的课程。您可以使用request.WriteBody方法将其写入XMLDictionaryWriter。或者简单地使用ToString,然后使用XDocument.Parse
yourServiceClient.Endpoint.EndpointBehaviors.Add(new SOAPEndpointBehaviour());