【问题标题】:Inspect the HttpWebRequest for WCF calls on client side?在客户端检查 WCF 调用的 HttpWebRequest?
【发布时间】:2014-12-18 18:37:17
【问题描述】:

我正在使用 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 做同样的事情)

【问题讨论】:

  • 那么,WCF 跟踪和消息记录还不够吗?
  • 您真的需要从Message 中提取各种信息吗?我唯一一次使用消息检查器,一个简单的request.ToString() 日志消息就足够了。
  • 看看这个 StackOverflow 答案,看看它是否符合您的预期stackoverflow.com/questions/1365295/…
  • @JohnSaunders 是的,我尝试过追踪,但确实符合我的需要。我需要记录我自己的数据(关于请求)和网络详细信息(msg 大小、时间、http 方法等)的组合。这是为了适应现有的日志记录系统。

标签: c# .net wcf client


【解决方案1】:

似乎是 Visual Studio 在附加调试器时创建的。

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

来源:Property httpRequest' not present

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多