问题
I'm trying to modify request in WCF service.
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
string xmlRequest = request.ToString();
XDocument xDoc = XDocument.Parse(xmlRequest);
//Some request modifications
//Here i have XML what in want to send
request = Message.CreateMessage(request.Version, request.Headers.Action, WhatHere?);
request.Headers.Clear();
return null;
}
But i don't know what i can set in CreateMessage
or maybe is different way to set my XML as request.
回答1:
You can pass an XmlReader object representing the modified message. Below is an example taken from the article How to inspect and modify WCF message via custom MessageInspector.
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
Console.WriteLine("====SimpleMessageInspector+BeforeSendRequest is called=====");
//modify the request send from client(only customize message body)
request = TransformMessage2(request);
return null;
}
//only read and modify the Message Body part
private Message TransformMessage2(Message oldMessage)
{
Message newMessage = null;
//load the old message into XML
MessageBuffer msgbuf = oldMessage.CreateBufferedCopy(int.MaxValue);
Message tmpMessage = msgbuf.CreateMessage();
XmlDictionaryReader xdr = tmpMessage.GetReaderAtBodyContents();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xdr);
xdr.Close();
//transform the xmldocument
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("a", "urn:test:datacontracts");
XmlNode node = xdoc.SelectSingleNode("//a:StringValue", nsmgr);
if(node!= null) node.InnerText = "[Modified in SimpleMessageInspector]" + node.InnerText;
MemoryStream ms = new MemoryStream();
XmlWriter xw = XmlWriter.Create(ms);
xdoc.Save(xw);
xw.Flush();
xw.Close();
ms.Position = 0;
XmlReader xr = XmlReader.Create(ms);
//create new message from modified XML document
newMessage = Message.CreateMessage(oldMessage.Version, null,xr );
newMessage.Headers.CopyHeadersFrom(oldMessage);
newMessage.Properties.CopyProperties(oldMessage.Properties);
return newMessage;
}
来源:https://stackoverflow.com/questions/35631372/wcf-iclientmessageinspector-beforesendrequest-modify-request