Keywords: Visual Studio | IISExpress | Port Conflict | ERR_CONNECTION_REFUSED | Debugging Error
Abstract: This technical paper provides an in-depth analysis of the ERR_CONNECTION_REFUSED error encountered during ASP.NET project debugging in Visual Studio. Focusing on port conflict identification and resolution, it offers systematic troubleshooting steps and code examples to help developers quickly diagnose and fix IISExpress connectivity issues.
Problem Background and Phenomenon Analysis
When debugging ASP.NET projects in Visual Studio on Windows Server 2008 environment, developers frequently encounter ERR_CONNECTION_REFUSED errors. This error manifests as inability to access IISExpress-hosted websites via localhost:xxxx, with browsers displaying "This webpage is not available" prompts. Detailed error information captured through Fiddler shows: "The socket connection to localhost failed. ErrorCode: 10061. No connection could be made because the target machine actively refused it 127.0.0.1:23162".
Root Causes of Port Conflicts
The essence of ERR_CONNECTION_REFUSED error is the inability to establish connection on target ports. In IISExpress environment, this is typically caused by: Port occupation by other processes: Background services or applications may have bound to the same port number. System firewall blocking connections: Windows firewall configurations might prevent inbound connections on specific ports. IISExpress configuration errors: Binding configurations in applicationhost.config file may contain conflicts.
Solution Implementation Steps
Based on validated effective methods, modifying project port numbers provides the most direct and efficient solution. The specific operational procedure is as follows:
First, right-click the project in Visual Studio and select "Properties". In the project properties window, navigate to the "Web" tab. Locate the "Project Url" field in the "Servers" section and change the original port number to a new unoccupied port. The critical step is clicking the "Create Virtual Directory" button, or confirming virtual directory creation when prompted by the system.
The following code example demonstrates how to dynamically check port availability in C# projects:
using System.Net;
using System.Net.Sockets;
public static bool IsPortAvailable(int port)
{
try
{
using (var client = new TcpClient())
{
client.Connect(IPAddress.Loopback, port);
return false;
}
}
catch (SocketException)
{
return true;
}
}
Configuration Verification and Optimization
After completing port modifications, verify the correctness of IISExpress configurations. Check site binding configurations in applicationhost.config file to ensure consistent protocol, port, and hostname settings. Example configuration is as follows:
<site name="WebApplication1" id="4">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\TFS-WorkRepository\Sandbox\WebApplication1\WebApplication1" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:23162:localhost" />
</bindings>
</site>
Auxiliary Troubleshooting Methods
Beyond port number modifications, the following auxiliary troubleshooting approaches can be employed: Use netsh commands to check IP listen lists, ensuring configuration as 0.0.0.0 to allow local connections. Delete and recreate applicationhost.config file in the .vs folder under project directory. Add HTTPS bindings for default website in IIS Manager to ensure complete SSL configuration.
Preventive Measures and Best Practices
To prevent recurrence of similar issues, the following preventive measures are recommended: Regularly monitor port usage using netstat commands. Establish clear port allocation rules in project development specifications. Create standardized IISExpress configuration templates. Implement automated environment health check mechanisms.
Through systematic fault diagnosis and standardized development practices, ERR_CONNECTION_REFUSED errors can be effectively resolved, ensuring stability and reliability of development environments.