gpt4 book ai didi

c# - 在反序列化之前获取 WCF 消息正文

转载 作者:行者123 更新时间:2023-11-30 17:59:02 24 4
gpt4 key购买 nike

我正在实现 WCF 服务,该服务公开了一个方法,其 [OperationContract][XmlSerializerFormat]。我有时会收到正文不是有效 XML 的请求。在这种情况下,我想记录原始主体,这样我就可以知道为什么它没有构成有效的 XML。但是,我无法从 Message 对象中获取它,请参阅我的尝试(通过实现 IDispatchMessageInspector 接口(interface)):

    public object IDispatchMessageInspector.AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
request.ToString(); // "... Error reading body: System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1. ..."
request.WriteBody(...); // Serialization Exception, also in WriteMessage and other Write* methods
request.GetReaderAtBodyContents(...); // Same
HttpRequestMessageProperty httpRequest = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]; // no body in httpRequest
}

在 watch 中查看时,request.messageData 似乎包含正文 - 但那是私有(private)成员。

如何在不尝试反序列化的情况下获取消息缓冲区?

最佳答案

是的,你需要custom MessageEncoder ,与消息检查器 (IDispatchMessageInspector/IClientMessageInspector) 不同,它会看到原始字节内容,包括任何格式错误的 XML 数据。

然而,实现这种方法并非易事。你必须包装一个标准的 textMessageEncoding作为定制binding element并调整config文件以使用该自定义绑定(bind)。

您还可以看到我是如何在我的项目中做到这一点的示例 - wrapping textMessageEncoding,记录 encoder , 自定义绑定(bind) elementconfig .

关于c# - 在反序列化之前获取 WCF 消息正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com