Я хочу получить сообщение мыльного конверта от клиента WCF, который я написал в XDocument.Итак, я написал свой собственный инспектор сообщений клиента:
public class MessageInspector : IClientMessageInspector { public object BeforeSendRequest(ref Message request, IClientChannel channel) { var xDocument = new XDocument(); using (var stream = new MemoryStream()) { using (var writer = XmlWriter.Create(stream)) { request.WriteMessage(writer); writer.Flush(); stream.Position = 0; xDocument.Save(stream); } } return request; } public void AfterReceiveReply(ref Message reply, object correlationState) { } }
И поведение конечной точки:
public class InspectorBehavior : IEndpointBehavior { public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(new MessageInspector()); } }
, которое я подключил к своему клиенту:
client.Endpoint.Behaviors.Add(new InspectorBehavior());
Моя проблема в том, что когда я отправляю сообщение, я получаю исключение из xDocument.Save (stream).В нем говорится:
EndDocument токена в состоянии Document приведет к неверному XML-документу.
Конверт SOAP выглядит для меня как действительный XML:
<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://localhost:8080/kestrel/AirService</Action> <h:SessionContext xmlns="http://www.mywebsite.com/soa/common/security/SessionContext_v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:h="http://www.mywebsite.com/soa/common/security/SessionContext_v1" /> </s:Header> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <MyMessage Foo="bar" xmlns="http://www.mywebsite.com/schema/air_v46_0"> </MyMessage> </s:Body> </s:Envelope>
Итакпочему я получаю эту ошибку и как ее предотвратить?