【发布时间】:2021-03-28 03:37:31
【问题描述】:
我使用Message.CreateMessage 创建Message 并将其发送到WCF 服务端点,但我创建的每条消息都以<?xml version="1.0" encoding="utf-16"?> 开头,这导致服务拒绝我的Operation is not supported 消息。 SOAP 1.1 standard 声明 The Envelope is the top element of the XML document representing the message. 这可能是服务拒绝我的消息的原因。不同的 MessageVersions 不会更改生成的 XML,构造函数确实接受 BodyWriter 但 XML 正文不会导致我的问题,它是根元素。
有没有一种方法可以在没有 XML 根元素的情况下生成带有 Message.CreateMessage 的 Message?
代码
internal static Message CreateGetMessage(string id)
{
const string Get = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get";
const string Rm = "http://schemas.microsoft.com/2006/11/ResourceManagement";
/* unnecessary code removed to create simplest example */
var message = Message.CreateMessage(MessageVersion.Default, Get);
if (message.Headers.FindHeader("ResourceReferenceProperty", Rm) <= 0)
{
message.Headers.Add(MessageHeader.CreateHeader("ResourceReferenceProperty", Rm, id));
}
return message;
}
生成的 SOAP 信封
<?xml version="1.0" encoding="utf-16"?>
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</a:Action>
<ResourceReferenceProperty xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">8f2a4ecc-e3dc-1a4c-5a28-4a4a7a8e6644</ResourceReferenceProperty>
</s:Header>
<s:Body />
</s:Envelope>
所需的 SOAP 信封
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</a:Action>
<ResourceReferenceProperty xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement">8f2a4ecc-e3dc-1a4c-5a28-4a4a7a8e6644</ResourceReferenceProperty>
</s:Header>
<s:Body />
</s:Envelope>
调试
【问题讨论】:
-
我发现删除 utf-16 错误的唯一方法是使用 readline 跳过处理该行。以以下答案为例:stackoverflow.com/questions/65331628/…
-
<?xml version="1.0" encoding="utf-16"?>不是根元素,它是XML declaration。我确实找到了WCF Change message encoding from Utf-16 to Utf-8 和Can you change the XML Declaration of a WCF Client Request? If so, how?,你可以检查一下是否有帮助。