httpmessagehandler - 阅读内容 [英] httpmessagehandler - reading content

查看:133
本文介绍了httpmessagehandler - 阅读内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的消息处理程序将登录请求和响应。最好我想

I created a message handler which will log the request and the response. ideally I want to

public class LoggingMessageHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        LogRequest(request);

        return base.SendAsync(request, cancellationToken).ContinueWith(task =>
        {
            var response = task.Result;

            LogResponse(response);

            return response;
        });
    }

    private void LogRequest(HttpRequestMessage request)
    {
        var writer = request.GetConfiguration().Services.GetTraceWriter();
        var content = request.Content;

        (content ?? new StringContent("")).ReadAsStringAsync().ContinueWith(x =>
        {
            writer.Trace(request, "request", System.Web.Http.Tracing.TraceLevel.Info, t =>
            {
                t.Message = x.Result;
            });
        });
    }

    private void LogResponse(HttpResponseMessage response)
    {
        var request = response.RequestMessage;
        var writer = request.GetConfiguration().Services.GetTraceWriter();
        var content = response.Content;

        (content ?? new StringContent("")).ReadAsStringAsync().ContinueWith(x =>
        {
            writer.Trace(request, "response", System.Web.Http.Tracing.TraceLevel.Info, t => 
            {
                t.Status = response.StatusCode;
                t.Message = x.Result;
            });
        });
    }
}

和这里是我的客户code。

and here is my client code.

public ActionResult Index()
{
    var profile = Client.GetAsync("Vendor").Result.EnsureSuccessStatusCode().Content.ReadAsAsync<VendorProfileModel>().Result;
    return View(profile);
}

日志似乎是工作。但是,当这个处理程序被注册我的客户code返回一个空的对象。如果我删除此处理模型成功地从响应读取并显示在屏幕上。

Logging appears to be working. However, when this handler is registered my client code returns an empty object. If I remove this handler the model is successfully read from the response and displayed on screen.

有没有办法阅读的内容,并显示在客户端上的结果?

Is there a way to read the content and display the results on the client?

推荐答案

几天后在网上我终于找到了问题的根源和解决办法挖左右。首先,问题:

after a few more days for digging around on the net I finally found the root problem and a solution. First the problem:


  1. 一切的WebAPI是异步

  2. 我的操作使用Controller.User这又是哪位Thread.CurrentPrinciple

  3. 我使用ITraceWriter作为我的日志抽象

显然没有在ITraceWriter mechanicism其中当前配置文件没有在线程之间传播的错误。因此,我失去了原则,当我到达我的控制器操作。因此,我的查询返回一个空的结果,而不是完全填充的结果。

apparently there is a bug in the ITraceWriter mechanicism where the current profile is not propagated across threads. therefore, i loose the principle when i get to my controller action. therefore, my query returns an empty result, rather than a fully populated result.

解决方案:不要使用ITraceWriter来记录消息。这本来是很好的使用内置的机制,但是,这并不工作。这里是链接到提供更详细的/背景相同的问题。

solution: don't use ITraceWriter to log messages. It would have been nice to use the built in mechanics, but that doesn't work. here is the link to the same issue which provides more detail/context.

的https://aspnetwebstack.$c$cplex.com/workitem/237

这篇关于httpmessagehandler - 阅读内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆