作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试修改 WCF 服务中的请求。
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;
}
但我不知道我可以在 CreateMessage
中设置什么,或者可能是将我的 XML 设置为请求的不同方式。
最佳答案
你可以传递一个 XmlReader表示修改消息的对象。以下是文章 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;
}
关于c# - WCF IClientMessageInspector.BeforeSendRequest 修改请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35631372/
使用 IClientMessageInspector 时接口(interface),我可以取消来自BeforeSendRequest 的请求吗?方法?返回 null无论如何发送请求。 public
我正在尝试修改 WCF 服务中的请求。 public object BeforeSendRequest(ref Message request, IClientChannel channel) {
我从 NVMS 下载了一个 C# 实现示例,以了解如何实现他们的 Web 服务。它使用 SOAP 请求来调用不同的服务。在源代码中,有一个实现 IEndpointBehavior 和 IClientM
我是一名优秀的程序员,十分优秀!