【问题标题】:Visual Studio 2017/IIS Express: Unable to connect to the configured development Web serverVisual Studio 2017/IIS Express:无法连接到已配置的开发 Web 服务器
【发布时间】:2019-09-12 18:23:51
【问题描述】:

保存正在进行的工作

尝试从 Visual Studio 2019(或 Visual Studio 2017 或 Visual Studio 2015)运行网站时,出现错误:

  • Visual Studio 2015:

    无法连接到已配置的开发 Web 服务器。

  • Visual Studio 2017:

    无法连接到已配置的开发 Web 服务器。

    IIS Express 的输出:
    正在启动 IIS Express...
    在应用程序“/”中成功为站点“WebSite3”注册 URL“http://localhost:59392/
    已完成网站“WebSite3”的注册
    IIS Express 正在运行。

  • Visual Studio 2019:

    无法连接到已配置的开发 Web 服务器。

    IIS Express 的输出: 启动 IIS Express ... 成功为站点“WebSite3”应用程序“/”注册 URL“http://localhost:59392/” 已完成网站“WebSite3”的注册 IIS Express 正在运行。

IISExpress 正在实际上正在运行和监听,但实际上没有任何效果:

你有什么尝试 - everything

我尝试过的事情(来自 Stackoverflow 上的所有其他问题):

  • netsh http add urlacl url=http://localhost:56997/ user=everyone
  • 以管理员身份运行 Visual Studio
  • 重启 Windows
  • 删除隐藏的.vs文件夹
  • 从不同的文件夹运行网站
  • 关闭 Windows 防火墙
  • 更改网站的端口
  • 更改网站的端口并点击创建虚拟目录
  • 从我的 Documents 文件夹中删除 IISExpress 文件夹
  • 全新安装 Visual Studio(我安装了 2019)

你可以看到 IISExpress 创建了一个监听套接字,并且 IISExpress 通知区域图标显示 IISExpress 认为该网站正在运行:

但它不会响应任何内容:

Bonus Chatter - Windows 内置内核模式 Web 服务器

IISExpress.exe 本身不会打开侦听套接字。

Windows 带有一个内置的内核模式迷你网络服务器:http.sys。您和 IISExpress.exe 通过调用以下方式使用此 Web 网络服务器:

//Initialize HTTP Server APIs
HttpInitialize(HTTPAPI_VERSION_1_0, HTTP_INITIALIZE_SERVER, null);

//Create a Request Queue
HANDLE requestQueue;
HttpCreateHttpHandle(ref requestQueue, 0);

/*
   Add URIs to listen on. We call HttpAddUrl for each URI.
   The URI is a fully qualified URI and must include the terminating (/) character.

   The IANA port numbers state ports 49152-65535 are for dynamic/private purposes.
   HttpAddUrl for localhost on a port >= 49152 works fine for non-admins.
*/
String url = "http://localhost:80/"; //Ports 1-1024 require administrator access

/*
   You can use netsh to modify the HttpServer api ACL to grant everyone acces s to port 80:

      netsh http add urlacl url=http://localhost:80/ user=EVERYONE listen=yes delegate=no

   But it is useful to note that WCF already has an "Everyone Allow" entry for port 80,
   as long as your URL starts with "/Temporary_Listen_Addresses/"

   WCF creates URLs of the form:

      http://+80/Temporary_Listen_Address/[random guid]/
*/
url = "http://+:80/Temporary_Listen_Addresses/{87CB7BDF-A52D-4496-AA1D-B6F60AC2841E}/"; //WCF style url

//Or we can just use one above 1024
url = "http://localhost:2113/";

将 URL 添加到您的请求队列中

//Add the url to our request queue    
ret = HttpAddUrl(requestQueue, url, null);

然后你设置一个循环来处理请求:

while (true)
{
   THTTP_REQUEST_ID requestID;
   Int32 requestBufferLength = sizeof(THTTP_REQUEST) + 16384;
   PHTTP_REQUEST request = GetMemory(requestBufferLength );
   DWORD bytesRead;

   ULONG res = HttpReceiveHttpRequest(requestQueue, 
                requestId,          // Req ID
                0,                  // Flags
                request,            // HTTP request buffer
                requestBufferLength,// req buffer length
                ref bytesRead,      // bytes received
                null                // LPOVERLAPPED
                );
   if (res == NO_ERROR)
   {
      res = SendHttpResponse(requestQueue, request, 451, "Totally not NSL", "I don't know what you mean ;)");
      if (res <> NO_ERROR) 
         break;

      // Reset the Request ID to handle the next request.
      requestID = 0;
   }
   else if (res == ERROR_MORE_DATA)
   {
      /*
         The input buffer was too small to hold the request headers.
         Increase the buffer size and call the API again.

         When calling the API again, handle the request that failed by passing a RequestID.
         This RequestID is read from the old buffer.
      */
      requestId = request.RequestId;

      //Free the old buffer and allocate a new buffer.
      requestBufferLength  = bytesRead;
      FreeMem(request);
      request = GetMemory(requestBufferLength);
   }
   else if ((result == ERROR_CONNECTION_INVALID) and (requestID <> 0)) 
   {
      /*
            The TCP connection was corrupted by the peer when attempting to handle a request with more buffer.
            Continue to the next request.
      */
      //Got invalid connection error
      requestID := 0;
   }
   else
   {
      // Other unhandled error; stopping processing requests
      break;
   }      
}

这完全是为了解释为什么是 System 正在监听,而不是 IISExpress.exe

如果您阅读 cmets,您会注意到为什么尝试执行 netsh http add urlacl 主要是货物崇拜编程;超过 1024 的端口不需要添加权限。

相关问题

【问题讨论】:

  • 我读到这个:最后,有人用所有适当的信息问了这个问题,我将向下滚动,看看实际的解决方案是什么......该死的。如果你有运气解决这个问题,我很想知道它是什么?

标签: visual-studio-2015 visual-studio-2017 visual-studio-2019


【解决方案1】:

在我的情况下,我在 nod32 smart scurity 中停止了防火墙并解决了问题!

【讨论】:

    【解决方案2】:

    我尝试了上面列出的所有方法,最后是 Windows 防火墙才是罪魁祸首。

    打开防火墙设置。关闭私有和公共防火墙。重新启动 Visual Studio。建造。跑步。有效。

    然后再重新开启防火墙,一步一步。

    【讨论】:

      【解决方案3】:

      我也遇到过同样的问题。但是当我刚刚从 IIS express 切换到本地 IIS(带有项目名称的那个)并返回 IIS express 时它得到了解决

      【讨论】:

        【解决方案4】:

        进入应用程序属性 --> 调试 --> 应用程序 URL 检查端口并更改端口并删除启用 SSL 中的勾号。这对我有用,希望对大家有帮助

        【讨论】:

          【解决方案5】:

          我在 Windows 10 机器上安装 docker 时遇到了同样的问题。作为副作用,hyper-v 被激活,并且一系列端口不再可用。 查看更多关于https://blog.sixthimpulse.com/2019/01/docker-for-windows-port-reservations/

          我的解决方案是打开“窗口和功能”并禁用 hyper-v

          显然,如果您需要运行 hyper-v,您还可以使用以下命令查看保留的 ip 范围列表:netsh int ipv4 show excludedportrange protocol=tcp 并简单地将您的项目配置为使用未保留的 IP 地址。

          【讨论】:

            猜你喜欢
            • 2018-04-14
            • 1970-01-01
            • 2021-10-12
            • 2018-01-14
            • 2020-02-09
            • 1970-01-01
            • 2021-08-06
            • 2019-02-11
            • 2020-10-15
            相关资源
            最近更新 更多