[英]Tracing the SOAP envelope that is being sent by a .NET web service client possible?
[英]WCF service client: Customized soap envelope not being sent on the wire
我从WCF客户端调用第三方Web服务。 问题是该服务需要特定的肥皂信封格式。
WCF Soap格式问题处理同样的问题,建议的解决方案是编写CustomTextMessageEncoder。
我编写了一个自定义消息编码器,并在WriteMessage方法中完成我的自定义格式化,采用MSDN示例编码器,如主题http://msdn.microsoft.com/en-us/library/ms751486(v=vs.110)中所述.aspx 。
自定义格式删除soap标头内容并添加第三方服务所需的一些名称空间前缀。
我可以看到在VS调试器中调用编码器并且它正在产生所需的输出:
public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
// code from MSDN example
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
message.WriteMessage(writer);
writer.Close();
// My customizing code
var targetStream = new MemoryStream();
FormatStreamToMirthFormat(stream, targetStream);
// code from MSDN example
// put the contents of the stream into a byte array
byte[] messageBytes = targetStream.GetBuffer();
int messageLength = (int)targetStream.Position;
stream.Close();
int totalLength = messageLength + messageOffset;
byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);
ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
return byteArray;
}
我已经在app.config中配置了服务消息跟踪,以查看实际发送的内容以及我遇到的问题是,看来传入的soap信封仍在线路上发送(使用basicHttpBinding时发送的信封) 。
为什么输入的肥皂信封而不是我的自定义肥皂信封被发送?
我注意到的另一件事是,如果我使用默认绑定“basicHttpBinding”,则线路上的消息包含一个Http标头,后跟soap信封:
<HttpRequest xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
<Method>POST</Method>
<QueryString></QueryString>
<WebHeaders>
<VsDebuggerCausalityData>uIDPo+hLJO6ZB9hNt7/+pVZglrYAAAAAus8ogaD9Y0O0ThkjxtbdzBlxO8Yn2yBJggkv3BRx/qsACQAA</VsDebuggerCausalityData>
</WebHeaders>
</HttpRequest>
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">..
而我的信息始于肥皂信封。
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
谢谢你的帮助。
MessageEncoder在WCF管道的后期发生,如果需要实现其他安全性要求,将来可能会遇到问题。 我推荐一个MessageFormatter。 看看这篇文章 。
它解释了如何执行此服务器端,但您也可以通过重写ApplyClientBehavior而不是ApplyDispatchBehavior,以及导出IClientMessageFormatter而不是IDispatchMessageFormatter,以类似的方式在客户端执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.