所以我相信我理解的概念,我想我應該用IClientMessageInspector
,但在我看來,所有的工具,我有一個System.ServiceModel.Channels.Message
的身體工作需要使用.NET中的XML對象,我不能這樣做,因爲, ,此消息的正文是非法的XML。最終,該消息是這樣的:如何在非法XML時清除服務的響應?
[random number]
<validXml />
[other random number]
例如:
379
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<ns1:getVersionResponse>
<ns1:getVersionResponse xsi:type="xsd:string">1.2.3</ns1:getVersionResponse>
</ns1:getVersionResponse>
</soapenv:Body>
</soapenv:Envelope>
0
這裏是將除了事實的工作,例如,上面是無效的XML(正是我的原因首先這樣做)!
public class CleanRandomNumbersInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
const int tenMegabytes = 10485760;
var buffer = reply.CreateBufferedCopy(tenMegabytes);
var copyToRead = buffer.CreateMessage();
string body;
using (var reader = copyToRead.GetReaderAtBodyContents())
{
body = reader.ReadOuterXml();
}
// Now that we have the text, modify it
body = body.Trim('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
// Shove it back into the message
var copyToPassOn = buffer.CreateMessage();
using (var stream = GenerateStreamFromString(body))
{
var writer = XmlDictionaryWriter.CreateTextWriter(stream);
copyToPassOn.WriteBodyContents(writer);
}
// Implement this method to inspect/modify messages after a message
// is received but prior to passing it back to the client
Console.WriteLine("AfterReceiveReply called");
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// GNDN
return null;
}
private static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
歸根結底,是的,我知道,這是一個糟糕的服務。然而,在另一方解決這個問題之前,我需要消耗它。我怎樣才能做到這一點?
嗯,你在這裏的東西。事實上,那個標題就在那裏。我所看到的是儘管服務返回了XML(帶有這些額外的位),但我的代理始終爲我提供了一個由服務方法返回的NULL對象,而不是任何類型的對象。我手動生成的代理沒有太多天賦('svcutil http://service.url?wsdl/l:cs/config:my.config')。當我開始調查爲什麼我的回答總是NULL時,「無效XML」是我的結論。所以我想是時候回過頭去調查爲什麼我的代理總是返回NULL ... – Jaxidian
對於它的價值,我相信這個第三方Web服務有一些搞砸的命名空間。 Ughh ..... – Jaxidian