WebApi委托处理程序 - 访问HttpRequestMessage

时间:2015-01-02 17:42:58

标签: c# asp.net-web-api

我使用以下代码:

protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    UriBuilder forwardUri = new UriBuilder(request.RequestUri);
    //remove the proxy port and replace with an Http port
    forwardUri.Port = 1671;
    //send it on to the requested URL
    request.RequestUri = forwardUri.Uri;
    HttpClient client = new HttpClient();
    if (request.Method == HttpMethod.Get)
        request.Content = null;
    try
    {

        var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
        if (response.StatusCode == HttpStatusCode.OK)
        {
            ManageOnline(response.RequestMessage);
        }
        return response;
    }
    catch (Exception ex)
    {
        throw;
    }
}

问题发生在ManageOnline方法中:

private void ManageOnline(HttpRequestMessage request)
{
    switch (request.RequestUri.PathAndQuery)
    {
        case @"/token":
            {
                //let's get Content Body contents here
                string jsonContent = request.Content.ReadAsStringAsync().Result;
                _cacheHandler.AddUser(jsonContent);
                break;
            }
        default:
            break;
    }
}   

我收到 request.Content.ReadAsStringAsync()&#39;抛出类型&#39; System.ObjectDisposedException&#39;

的例外

但是,如果我在ManageOnline(response.RequestMessage);运行之前运行var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

无论如何要克服这个问题吗?

非常感谢

0 个答案:

没有答案