c# - 在客户端检查 WCF 调用的 HttpWebRequest?

标签 c# .net wcf client

我正在使用 WCF 客户端代理来调用 Web 服务。我正在使用 IClientMessageInspector 向每个请求添加日志记录。我想从底层 HTTP 消息中获取信息,发现我可以通过以下方式获取它:

public class WCFLoggingInspector : IClientMessageInspector
{        
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var httpRequest = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        var method = httpRequest.Method;
        return null;
    }
}

在调试器中逐步执行时,这工作正常。但是,正常运行时,属性索引器出现错误:“名称为‘httpRequest’的属性不存在”

任何人都可以解释这里发生了什么,或者获取 HttpRequest 的替代方法吗? (我也在为 AfterReceiveReply 做同样的事情)

最佳答案

似乎 Visual Studio 在附加调试器时创建了它。

if (!properties.ContainsKey(HttpRequestMessageProperty.Name))
                properties.Add("httpRequest", new HttpRequestMessageProperty());

来源:Property httpRequest' not present

关于c# - 在客户端检查 WCF 调用的 HttpWebRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553435/

相关文章:

.NET 编译器——调试与发布

c# - Dump() 对象到 JSON pretty-print 字符串

c# - JQuery 按钮未通过 ajax 触发服务调用

.net - 为了让 WCF 传递大字符串,在哪里增加 maxStringContentLength?

c# - MEF 阻止类被手动实例化

c# - 插入多个值并返回多个值

c# - 领域模型与数据库模型不同吗?

c# - 反射返回对象中的 SetValue 与目标类型不匹配

c# - 即使在 CallBase = true/false 之后,原始方法仍然在 Moq 中被调用

.net - 你会在线程池线程上使用 await 关键字吗