Asked by:
httpclient postAsync -> Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'.

Question
-
User632428103 posted
Hello all,
we have an app under xamarin android build with visual studio 2017.
this app works since three years without any problems ...
since two weeks and I don't know why actually some device can't sync with our back end.
It's really strange because nothing has change in this part ...
this error does not appear on all devices but on one or two from time to time
If i put a break point inside the postAsync I have an exception with this -> Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream
Any one has an idea about how to solve this ?
also what does it mean ?
Here is it the code of the postAsync method :
public override HttpResult ExecutePost(Uri target, string body) { var client = new HttpClient(); client.MaxResponseContentBufferSize = MaxHttpResponseBufferSize; try { var requestContent = new StringContent(body, RequestContentEncoding, RequestContentType); var response = client.PostAsync(target, requestContent).Result; if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().Result; return new HttpResult(content, null, null); } return new HttpResult(null, "Empty response received for remote POST invokation", response.StatusCode.ToString()); } catch (Exception e) { return new HttpResult(null, "An error occured on HttpPostAsync", e.Message); } }
thanks a lot for your time
Tuesday, June 9, 2020 10:06 AM
All replies
-
User475983607 posted
I believe it has to do with how you are using HttpClient. You should have one HttpClient instance per service. See the docs.
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1
Tuesday, June 9, 2020 10:30 AM -
User632428103 posted
Hello mgebhard,
thanks for the link you provide but I can't understand WHY it's work on more > 100 device every day without problem at all
Just one device every week but not the same do not work ...
I've try to up the buffer / try to sync in 3G / delete special character in json / ...
but nothing work actually
I think I have no choice to test the static method ..
Ok I will check
I come after test
thanks
Wednesday, June 10, 2020 9:49 AM -
User475983607 posted
thanks for the link you provide but I can't understand WHY it's work on more > 100 device every day without problem at all
Just one device every week but not the same do not work ...
The best the community can do when we are unable to reproduce an issue is provide a code review.
There are other issues besides not using HttpClient as intended. The design blocks the main thread. Using .Result creates a new thread to handle the asynchronous call while blocking the main thread. The ExecutePost method should be changed to async and async should be used from top to bottom. Meaning ExcutePost callers should follow the async/await pattern as well.
The code shown is clearly a proxy. The proxy design copies from input buffers to a string then re-buffers the string to send the response. This is an inefficient use of buffers. I expect network exception with any significant pressure on the server using this design.
Wednesday, June 10, 2020 10:59 AM -
User632428103 posted
Hello,
it's SOLVEDDDDDDDD :)
we have add the database to the test.
Get the json and uses postman -> send -> result -> ENTITY TOO LARGE !
the size of the json is > 1.2 mega
Update IIS request filtering with value 2 000 000 -> 2 mega -> test again -> DONE :)
well i'm so happy :)
thanks for your time the problem it's solve
Wednesday, June 10, 2020 2:10 PM