title: Cannot access a disposed object (System.Net.Sockets.NetworkStream) on HttpClient
body: <p><strong>Updated 03/28/2017:</strong></p>
  
  <p>This is a piece of C# code in my Xamarin PCL project running on iOS.</p>
  
  <pre><code>public async Task&lt;string&gt; uploadPhoto(string imageBase64)
  {
  
        string responseContent = null;
        var uri = new Uri(Constants.uploadImageUrl);
  
        try
        {
             StringContent stringContent = new StringContent("photo=;base64," + imageBase64, Encoding.UTF8, "application/x-www-form-urlencoded");
  
             var response = await client.PostAsync(uri, stringContent).ConfigureAwait(false); 
  
             // always check return success first
             if (response.IsSuccessStatusCode)
             {
                  responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
             }
             else
             {
                  return null;
             }
  
  
             if (responseContent.Contains("Failed"))
             {
                 return null;
             }
         }
         catch (Exception ex)
         {
             Debug.WriteLine(@"uploadPhoto        ERROR {0}", ex.Message);
             if (ex.InnerException != null)
             {
                 Debug.WriteLine(@"uploadPhoto        ERROR {0}", ex.InnerException.Message);
             }
             return null;
         }
  
         return responseContent;
  }
  </code></pre>
  
  <p>Exception thrown by client.PostAsync();</p>
  
  <blockquote>
    <p>[0:] Cannot access a disposed object. Object name:
    'System.Net.Sockets.NetworkStream'.</p>
  </blockquote>
  
  <p>client.PostAsync() sometimes fails, it is not easy for me to replicate this exception.
  I have no idea why PostAsync() would throw such exception. My internet connection was good, uri and content were validated.</p>
  
  <p>Questions are:</p>
  
  <ol>
  <li><p>Why PostAsync() throwing this exception when everything was good?</p></li>
  <li><p>How System.Net.Sockets.NetworkStream play it's role in this exception?</p></li>
  </ol>
  
  <p>I hope someone out there can help answer these questions, and hopefully provide some guidance and light on solving this issue.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Here is the stack trace captured.
  Exception thrown by client.PostAsync().</p>
  
  <pre><code>{System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'System.Net.Sockets.NetworkStream'.
    at System.Net.WebConnectionStream.EndWrite (System.IAsyncResult r) [0x000d3] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/System/System.Net/WebConnectionStream.cs:616 
    at System.IO.Stream.&lt;BeginEndWriteAsync&gt;m__8 (System.IO.Stream stream, System.IAsyncResult asyncResult) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/io/stream.cs:729 
    at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1[TResult,TInstance].Complete (TInstance thisRef, System.Func`3[T1,T2,TResult] endMethod, System.IAsyncResult asyncResult, System.Boolean requiresSynchronization) [0x00002] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:1441 
  --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:447 
    at System.Net.Http.HttpClientHandler+&lt;SendAsync&gt;c__async0.MoveNext () [0x00308] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:371 
  --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in &lt;54e129cad3ee4c61a6b46c3680ddc69a&gt;:0 
    at System.Net.Http.HttpClient+&lt;SendAsyncWorker&gt;c__async0.MoveNext () [0x000a9] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276 
  --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.123/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in &lt;54e129cad3ee4c61a6b46c3680ddc69a&gt;:0 
    at MyApp.RestService+&lt;uploadPhoto&gt;d__4.MoveNext () [0x00098] in D:\MyApp\MyApp\RestService.cs:147 }
  </code></pre>