Refit: ObjectDisposedException throw when access to request content in ApiException

Created on 23 Aug 2019  路  8Comments  路  Source: reactiveui/refit

Is your feature request related to a problem? Please describe.

On request error (not success status code) an ApiException is throw.
When catching it for analysing, it's not possible to access the request content (of a POST for example).

try {
    // this doesn't return a 200 status code
    await RefitInterface.makePostCall(payload);
} catch (ApiException ex) {
   /* Imagine this try/catch in a middleware so it can't access the payload 
      passed to RefitInterface.makePostCall

       This line throws ObjectDisposedException
   */
   var requestPayload = await ex.RequestMessage.Content.ReadAsStringAsync();
}

Describe the solution you'd like
I identified the problem located in RequestBuilderImplementation::BuildCancellableTaskFuncForMethod, the finally block runs dispose on the request. I'm sorry but I don't know if this line is mandatory so I'm not able to suggest a solution to this problem.

Most helpful comment

Can you use ApiException.Content instead?

No, ApiException.Content is the content of the response. We want to read the content of the request message. So we can check the settings of the JSON serializer or make sure that the properties are written correctly.

All 8 comments

I'd also like to know the solution to this issue.

I have the same issue. Debugging is very difficult.

Can you use ApiException.Content instead?

Can you use ApiException.Content instead?

No, ApiException.Content is the content of the response. We want to read the content of the request message. So we can check the settings of the JSON serializer or make sure that the properties are written correctly.

Any solution to this? Just ran into it myself.

I've made a middleware for logging all exceptions and am handling an ApiException. However, when trying to await exception.RequestMessage.Content.ReadAsStringAsync() I get the exception mentioned by OP. same if I try to .Content.CopyToAsync(anotherStream)

It seems httpRequest.EnableBuffering() should be called, by that's no longer accessible through ApiException, because it only references HttpRequestMessage.

Closing due to age. Please try Refit v6 and reopen if still an issue.

@jbardon can you reopen the issue?

@clairernovotny The problem still exists in the latest version. (v6.0.15)
catch (ApiException aex) { var requestJson = await aex.RequestMessage.Content.ReadAsStringAsync(); }

Throws ObjectDisposedException

ex {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Http.PushStreamContent'. at System.Net.Http.HttpContent.CheckDisposed () [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cor鈥

"Cannot access a disposed object.\nObject name: 'System.Net.Http.PushStreamContent'."

Thanks for testing on the latest version @Px7-941.

It looks like I can't open the issue again because it's not me who closed it.
Can you reopen it @clairernovotny?

Was this page helpful?
0 / 5 - 0 ratings