Modified logic that downloads content to provide better diagnostic information upon failure.

This commit is contained in:
Michael Simons (VISUAL STUDIO) 2016-08-25 11:41:24 -05:00
parent 11b666acee
commit 7914b63474

View file

@ -125,6 +125,36 @@ function Load-Assembly([string] $Assembly) {
} }
} }
function GetHTTPResponse([Uri] $Uri)
{
$HttpClient = $null
try {
# HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet.
Load-Assembly -Assembly System.Net.Http
$HttpClient = New-Object System.Net.Http.HttpClient
$Response = $HttpClient.GetAsync($Uri).Result
if (($Response -eq $null) -or (-not ($Response.IsSuccessStatusCode)))
{
$ErrorMsg = "Failed to download $Uri."
if ($Response -ne $null)
{
$ErrorMsg += " $Response"
}
throw $ErrorMsg
}
return $Response
}
finally {
if ($HttpClient -ne $null) {
$HttpClient.Dispose()
}
}
}
function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [string]$CLIArchitecture) { function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [string]$CLIArchitecture) {
Say-Invocation $MyInvocation Say-Invocation $MyInvocation
@ -136,11 +166,7 @@ function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [str
$VersionFileUrl = "$AzureFeed/Sdk/$AzureChannel/latest.version" $VersionFileUrl = "$AzureFeed/Sdk/$AzureChannel/latest.version"
} }
try { $Response = GetHTTPResponse -Uri $VersionFileUrl
# HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet.
Load-Assembly -Assembly System.Net.Http
$HttpClient = New-Object System.Net.Http.HttpClient
$Response = $HttpClient.GetAsync($VersionFileUrl).Result
$StringContent = $Response.Content.ReadAsStringAsync().Result $StringContent = $Response.Content.ReadAsStringAsync().Result
switch ($Response.Content.Headers.ContentType) { switch ($Response.Content.Headers.ContentType) {
@ -148,13 +174,6 @@ function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [str
{ ($_ -eq "text/plain") } { $VersionText = $StringContent } { ($_ -eq "text/plain") } { $VersionText = $StringContent }
default { throw "``$Response.Content.Headers.ContentType`` is an unknown .version file content type." } default { throw "``$Response.Content.Headers.ContentType`` is an unknown .version file content type." }
} }
}
finally {
if ($HttpClient -ne $null) {
$HttpClient.Dispose()
}
}
$VersionInfo = Get-Version-Info-From-Version-Text $VersionText $VersionInfo = Get-Version-Info-From-Version-Text $VersionText
@ -330,19 +349,16 @@ function Extract-Dotnet-Package([string]$ZipPath, [string]$OutPath) {
} }
function DownloadFile([Uri]$Uri, [string]$OutPath) { function DownloadFile([Uri]$Uri, [string]$OutPath) {
$Stream = $null
try { try {
# HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet. $Response = GetHTTPResponse -Uri $Uri
Load-Assembly -Assembly System.Net.Http $Stream = $Response.Content.ReadAsStreamAsync().Result
$HttpClient = New-Object System.Net.Http.HttpClient
$Stream = $HttpClient.GetAsync($Uri).Result.Content.ReadAsStreamAsync().Result
$File = [System.IO.File]::Create($OutPath) $File = [System.IO.File]::Create($OutPath)
$Stream.CopyTo($File) $Stream.CopyTo($File)
$File.Close() $File.Close()
} }
finally { finally {
if ($HttpClient -ne $null) {
$HttpClient.Dispose()
}
if ($Stream -ne $null) { if ($Stream -ne $null) {
$Stream.Dispose() $Stream.Dispose()
} }