我无法通过WCF自定义适配器(wsHTTP绑定)推送消息。我正在使用自定义端点行为扩展MessageInspector从SOAP信封中删除MessageID,ReplyTo,To&Action节点。调试终结点行为类时没有显示任何错误,但我不断收到BizTalk NACK的“尝试传输消息时遇到内部服务器错误”错误。端点行为类的BeforeSendRequest方法中的代码是:
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
try
{
MessageBuffer messageBuffer = request.CreateBufferedCopy(Int32.MaxValue);
Message message = messageBuffer.CreateMessage();
messageBuffer.Close();
MemoryStream msOriginal = new MemoryStream();
XmlWriter xwOriginal = XmlWriter.Create(msOriginal);
message.WriteMessage(xwOriginal);
xwOriginal.Flush();
string originalMsg = Encoding.UTF8.GetString(msOriginal.ToArray());
xwOriginal.Close();
msOriginal.Position = 0;
msOriginal = new MemoryStream(Encoding.UTF8.GetBytes(originalMsg));
XmlDictionaryReader xdrOriginal = XmlDictionaryReader.CreateTextReader(msOriginal, new XmlDictionaryReaderQuotas());
XDocument xdoc = XDocument.Load(xdrOriginal);
var rootMsgElement = xdoc.Root;
//string rootNameSpace = "http://www.w3.org/2003/05/soap-envelope"; // string.Empty;
string nameSpace = "http://www.w3.org/2005/08/addressing";
//if (rootMsgElement.Name.LocalName == XName.Get("Envelope", rootNameSpace))
//{
if (!string.IsNullOrEmpty(xdoc.Descendants(XName.Get("Action", nameSpace)).First().Value) || !string.IsNullOrEmpty(xdoc.Descendants(XName.Get("MessageID", nameSpace)).First().Value) || !string.IsNullOrEmpty(xdoc.Descendants(XName.Get("ReplyTo", nameSpace)).First().Value) || !string.IsNullOrEmpty(xdoc.Descendants(XName.Get("To", nameSpace)).First().Value))
{
xdoc.Descendants(XName.Get("Action", nameSpace)).Remove();
xdoc.Descendants(XName.Get("MessageID", nameSpace)).Remove();
xdoc.Descendants(XName.Get("ReplyTo", nameSpace)).Remove();
xdoc.Descendants(XName.Get("To", nameSpace)).Remove();
}
//}
string updatedMsgString = rootMsgElement.ToString();
// xdoc.Save(updatedMsgString);
Message recreatedNewMessage = null;
msOriginal = new MemoryStream(Encoding.UTF8.GetBytes(updatedMsgString));
msOriginal.Position = 0;
XmlDictionaryReader newXdr = XmlDictionaryReader.CreateTextReader(msOriginal, new XmlDictionaryReaderQuotas());
recreatedNewMessage = Message.CreateMessage(newXdr, int.MaxValue, request.Version);
recreatedNewMessage.Properties.CopyProperties(request.Properties);
request = recreatedNewMessage;
}
catch (Exception ex)
{
string errorMsg = ex.Message;
if (ex.InnerException != null)
{
errorMsg += ex.InnerException.Message;
}
}
return request;
}
如您所见,我尝试将流的位置设置回0,但仍然无济于事。我还尝试过将没有sans MessageID,ReplyTo,To&Action节点的消息直接推送到端点,并获取正确的响应。让我知道我是否在这里丢失任何东西。