1

我无法通过 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,但仍然无济于事。我还尝试将没有 MessageID、ReplyTo、To 和 Action 节点的消息直接推送到端点,它会获取正确的响应。让我知道我是否在这里遗漏了什么。

4

0 回答 0