HttpClient and stream issues
System.Net.Http.StreamContent
and System.IO.FileStream
are instantiated but not disposed.
Add the code to invoke Dispose
like you did in finally
block.
try{ # enclose the program}finally{ if($null -ne $streamContent) { $streamContent.Dispose() } if($null -ne $packageFileStream) { $packageFileStream.Dispose() }}
That exception is clearly threading-related.
Your method calls use async method calls, but not correctly:
.ReadAsStringAsync().GetAwaiter().GetResult()
just blocks the current thread while waiting for another: it actually takes up an additional thread.
So, because you're a) not using threading in a useful fashion and b) getting an exception for your efforts, I suggest you take anywhere in your code which uses that construct and replace it with a normal method call.
Edit:
Threading issues aside, you could try
$content.Add($streamContent)$content.LoadIntoBufferAsync().GetAwaiter().GetResult()
to force the content stream to load prior to the next async post operation.
Be careful - this NotSupportedException
can mask a different error, such as an authorization problem (e.g. incorrect credentials passed in the NetworkCredential
object).
I found this out by running Fiddler and watching the responses.