ObjectDisposedException in HttpClient

I have a generic Windows project with several API calls. One method refuses to work because my other calls work exactly like that. I tried the using keyword to solve the problem.

Function:

 public async Task<User> GetNewUser(string user_guid, OAuthTokens OAuth) { String userguidJSON = VALIDJSON_BELIEVE_ME; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", Encrypt(OAuth.Accesstoken)); using (HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, BASE_URL + URL_USERS + "/data")) { req.Content = new StringContent(userguidJSON, Encoding.UTF8, "application/json"); await httpClient.SendAsync(req).ContinueWith(respTask => { Debug.WriteLine(req.Content.ReadAsStringAsync()); //Error is thrown ono this line }); return null; } } } 

EDIT

 public async Task<User> GetNewUser(string user_guid, OAuthTokens OAuth) { String userguidJSON = VALIDJSON_BELIEVE_ME; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", Encrypt(OAuth.Accesstoken)); using (HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, BASE_URL + URL_USERS + "/data")) { req.Content = new StringContent(userguidJSON, Encoding.UTF8, "application/json"); await httpClient.SendAsync(req); var result = await req.Content.ReadAsStringAsync(); //Cannot access a disposed object. Object name: 'System.Net.Http.StringContent'. Debug.WriteLine(result); return null; } } } 

Stacktrace element

  at System.Net.Http.HttpContent.CheckDisposed() at System.Net.Http.HttpContent.ReadAsStringAsync() at Roadsmart.Service.RoadsmartService.<GetNewUser>d__2e.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Roadsmart.ViewModel.SettingsPageViewModel.<SetNewProfilePicture>d__1e.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state) at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore() 
+6
source share
3 answers

ObjectDisposedException thrown because you remove the HttpRequestMessage and HttpClient until req.Content.ReadAsStringAsync() .

Note that req.Content.ReadAsStringAsync() is an asynchronous method. You need to wait for completion before uninstalling HttpClient .

Also, you seem to be calling ReadAsStringAsync in req.Content , shouldn't be response.Content ?

 using (HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, BASE_URL + URL_USERS + "/data")) { req.Content = new StringContent(userguidJSON, Encoding.UTF8, "application/json"); var response = await httpClient.SendAsync(req); var result = await response.Content.ReadAsStringAsync();//await it Debug.WriteLine(result); return null; } 

There is practically no reason to use ContinueWith when working with async / await. Everything the compiler does for you.

+9
source

You get access to the contents of the request, not the answer.

it

 await httpClient.SendAsync(req); var result = await req.Content.ReadAsStringAsync(); //Cannot access a disposed object. Object name: 'System.Net.Http.StringContent'. 

it should be

 var response = httpClient.SendAsync(req); var result = await response.Content.ReadAsStringAsync(); 
+4
source

The actual reason that an HttpClient is HttpClient is because the HttpClient uses Content immediately after the request is complete. Check out the docs .

So, if you need to read Request content, for example, in tests, be sure to read it before calling SendAsync .

+4
source

Source: https://habr.com/ru/post/984501/


All Articles