One-Click Publish in Visual Studio showing an error The underlying connection was closed: An unexpected error occurred on a send

Challenge:

If you are using Visual Studio one click publish, and you are facing following error (We were getting it while doing publish from Build server to Target server.)
Web deployment task failed. (Could not complete the request to remote agent URL ‘https://<HOSTNAME/IP>:8172/msdeploy.axd?site=<OURWEBSITE>’.)
This error indicates that you cannot connect to the server. Make sure the service URL is correct, firewall and network settings on this computer and on the server computer are configured properly, and the appropriate services have been started on the server.
Error details: Could not complete the request to remote agent URL ‘https://<HOSTNAME/IP>:8172/msdeploy.axd?site=<OURWEBSITE>’.
The underlying connection was closed: An unexpected error occurred on a send.Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
An existing connection was forcibly closed by the remote host                   0              0              WEBSITENAME
Or any other error related to Web Deploy [a.k.a. MsDeploy] technology used behind the scenes of Visual studio publish. Then this post is for you!

Solution:

This is the error, Which took days and days for us. Because all settings were working since couple of years and suddenly it stopped working. Without getting any clue. This is what we did for troubleshooting:

  1. Disable Firewall/Antivirus, Allow Port 8172
  2. Make sure Hostname/IP gets resolved
  3. Server’s IP and Port number is reachable : Yes – We verified it using Telnet and it can connect to Port 8172  and also verified Firewall rules as well – All good!
  4. Verified all Web deploy related services are fine or not – and they are fine – We tried to run Powershell script given by Microsoft in Reference Link #2 – But no luck
  5. Verified username and password are fine – They are fine – Verified it using remote desktop with those credentials
  6. Reinstalled MSDeploy on SBX – and It didn’t helped
  7. No any log entry in MS Deploy IIS Log [HEAD/POST]
  8. Tried with MSDeploy command : msdeploy.exe -verb:dump -source:iisapp=”Default Web Site”,computername=https://:8172/msdeploy.axd?site=Default%20Web%20Site,username=,password=,authType=basic  -verbose –whatif
  9. Fired MS deploy command from my local – as given in earlier email and monitored packets on SBX server using TCPView and netstat command : Found that : TCP Packet gets sent on 8172 and status : SYNC_RCVD – and then it drops packet

During this whole process. This is what we understood:
– Web Management Service listens on 8172
– If request received it calls MsDeploy.axd
Somehow, packet gets reached to Machine – But after that it should forward it to IIS – And in turn IIS will send it to MsDeploy – And looking at log files – Packet is being dropped at machine level

  1. http://forums.iis.net/t/1227905.aspx?The+underlying+connection+was+closed+An+unexpected+error+occurred+on+a+send+
  2. http://stackoverflow.com/questions/12959501/web-deployment-task-failed-when-using-webdeploy-in-vs2012
  3. http://www.iis.net/learn/publish/troubleshooting-web-deploy/troubleshooting-web-deploy-problems-with-visual-studio
  4. http://stackoverflow.com/questions/5841370/cant-get-my-ec2-windows-server-2008-web-stack-instance-to-receive-publishings
  5. http://stackoverflow.com/questions/11479927/visual-studio-2012-web-deploy-to-windows-server-2008-r2-with-iis-7-and-msdeploy
  6. http://www.asp.net/web-forms/overview/deployment/configuring-server-environments-for-web-deployment/configuring-a-web-server-for-web-deploy-publishing-(web-deploy-handler
  7. http://stackoverflow.com/questions/10894671/msdeploy-fails-for-webdeploy
  8. http://blogs.msdn.com/b/amol/archive/2011/02/09/errors-seen-while-using-msbuild-to-deploy-on-a-remote-iis-server-and-their-solutions.aspx

We raised ticket with Microsoft Support folks and this has been fixed by changing Server URL to : http://<HOSTNAME/IP>  from : https://<HOSTNAME/IP>:8172/msdeploy.axd [Reference link : https://msdn.microsoft.com/en-us/library/dd465337%28v=vs.110%29.aspx]
To be honest — We haven’t been able to find out why earlier URL was working earlier and not working now!