In Focus

TFS Build Error: Set-AzureDeployment

In this article you will learn how to solve the error: Set-AzureDeployment : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

Recently I encountered a strange issue and figured out a solution as well so I decided to share it because I didn't find much over the web about it.

We have created the TFS Build System built for an on-premise TFS server. We are using it for continuous deployment on Windows Azure Cloud Services and everything is working fine. There is an excellent documentation available on the MSDN so you can have a look at it.

What the error was

The issue occured when the build stopped working. Prior builds were successful but we began getting an issue with each new build.

The Error Message was: Set-AzureDeployment: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

Actually in the background the build process has a workflow and it tries to execute an PowerShell command on the build server for the deployment of your cloud services in Windows Azure.

The solution

The way the build process workflow works is that it needs the .publishsetting file for doing the deployments on your Azure Cloud Services. All you need to do is, download the .publishsetting file for your Azure subscription (you can click here) and save it locally. Now go to the build server and replace your old .publishsetting file with this new one.

Now try queuing your builds; everything should work fine (if It had worked before :) ).

If you are not using the build system but only using the PowerShell command, in other words Set-AzureDeployment, to do the deployment then you can also opt the same solution, in other words just download and replace your old .publishsetting file with the new one.

Another solution

Now, even though the solution mentioned above worked for me fine,I encountered a similar issue in another environment a few days after I wrote this article and the preceding solution didn't work so I had to search for another solution and I found a workaround.

So I decided to edit the blog post and document this workaround so that it helps someone.

The changes needs to be done in the script, what this changes is, try to upload the cspkg file to blob storage and the create / update the deployment using blob URL.

You can define the new variable $containerName at the top of your file and initialize it with some string value that will act as a container name.

If the target container does not already exist, create it.

  1. $containerState = Get-AzureStorageContainer -Name $containerName -ea 0  
  2. if ($containerState -eq $null)  
  3. {  
  4.    New-AzureStorageContainer -Name $containerName | out-null  
  5. }  
  6.   
  7. Set-AzureStorageBlobContent -File $packageLocation -Container $containerName -Blob $blob -Force| Out-Null  
  8.   
  9. $script:packageLocation = (Get-AzureStorageBlob -blob $blobName -Container $containerName).ICloudBlob.uri.AbsoluteUri  
I hope this helps someone.