如何确定哪些IIS Express实例正在使用端口? [英] How to figure out what IIS Express instance is using a port?

查看:95
本文介绍了如何确定哪些IIS Express实例正在使用端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式杀死正在占用特定端口的正在运行的IIS实例,但似乎无法确定使用特定端口的IIS实例。

I want to kill a running IIS Instance programmatically that is occupying a specific port, but it seems there is no way to figure out what IIS Instance is using a specific port.

netstat.exe只显示进程正在使用PID 4,但那是系统进程。 netsh http show urlacl根本不显示占用的端口。

netstat.exe just shows that the process is having the PID 4, but that's the system process. "netsh http show urlacl" does not display the occupied port at all.

IIS Express Tray程序以某种方式知道这一点。当我尝试在端口被占用时启动另一个IIS Express实例时,我收到以下错误:

端口'40000'已被进程'IIS Express'使用(进程ID'10632 ')。

The IIS Express Tray program knows this somehow. When I try to start another IIS Express instance while the port is occupied I get the following error:
"Port '40000' is already being used by process 'IIS Express' (process ID '10632').

任何人都知道如何获取这些信息?

Anyone got a clue how I can get this information?

推荐答案

似乎PID是4(系统),因为实际的监听套接字是在名为 http

It seems like the PID is 4 (System) because the actual listening socket is under a service called http.

我看了一下iisexpresstray.exe是什么用于提供所有正在运行的IISExpress应用程序的列表。幸运的是,它的托管.NET代码(全部在iisexpresstray.dll中)很容易被反编译。

I looked at what iisexpresstray.exe was using to provide a list of all running IISExpress applications. Thankfully it's managed .NET code (all in iisexpresstray.dll) that's easily decompiled.

它似乎至少有三个获取流程的端口号的不同方法:

It appears to have at least three different ways of getting the port number for a process:


  1. 从<读取 / port 命令行参数(我们知道不可靠)

  2. 运行 netsh http show servicestate view = requestq 并解析输出

  3. 调用 Microsoft.Web.RuntimeStatusClient.GetWorkerProcess(pid)并解析网站网址

  1. Reading /port from the command-line arguments (unreliable as we know)
  2. Running netsh http show servicestate view=requestq and parsing the output
  3. Calling Microsoft.Web.RuntimeStatusClient.GetWorkerProcess(pid) and parsing the site URL

不幸的是,大多数在iisexpresstray.dll中有用的东西,如 IisExpressHelper 类被声明为 internal (虽然我想有生成包装的工具或复制程序集并公布所有内容。)

Unfortunately, most of the useful stuff in iisexpresstray.dll like the IisExpressHelper class is declared internal (although I imagine there're tools to generate wrappers or copy the assembly and publicize everything).

我选择使用Microsoft.Web.dll。它在我的GAC中,虽然由于某种原因没有出现在Visual Studio中作为参考添加的程序集列表中,所以我只是从我的GAC复制了该文件。一旦我有了Microsoft.Web.dll,只需要使用这段代码:

I opted to use Microsoft.Web.dll. It was in my GAC, though for some reason wasn't appearing in the list of assemblies available to add as a reference in Visual Studio, so I just copied the file out from my GAC. Once I had Microsoft.Web.dll it was just a matter of using this code:

    using (var runtimeStatusClient = new RuntimeStatusClient())
    {
      var workerProcess = runtimeStatusClient.GetWorkerProcess(process.Id);
      // Apparently an IISExpress process can run multiple sites/applications?
      var apps = workerProcess.RegisteredUrlsInfo.Select(r => r.Split('|')).Select(u => new { SiteName = u[0], PhysicalPath = u[1], Url = u[2] });
      // If we just assume one app
      return new Uri(apps.FirstOrDefault().Url).Port;
     }

您也可以拨打 RuntimeClient.GetAllWorkerProcesses 只检索实际的工作进程。

You can also call RuntimeClient.GetAllWorkerProcesses to retrieve only actual worker processes.

我查看 RegisteredUrlsInfo (在Microsoft.Web.dll中) )同时发现它使用了两个COM接口,

I looked into RegisteredUrlsInfo (in Microsoft.Web.dll) as well and found that it's using two COM interfaces,


  1. IRsca2_Core F90F62AB-EE00-4E4F-8EA6-3805B6B25CDD

  2. IRsca2_WorkerProcess B1341209-7F09-4ECD-AE5F-3EE40D921870

  1. IRsca2_Core (F90F62AB-EE00-4E4F-8EA6-3805B6B25CDD)
  2. IRsca2_WorkerProcess (B1341209-7F09-4ECD-AE5F-3EE40D921870)

最后,我读了一篇关于a Microsoft.Web.Administration的版本显然能够读取IISExpress应用程序信息,但信息非常稀缺,我在系统上找到的那个甚至不能让我实例化 ServerManager 没有管理员权限。

Lastly, I read about a version of Microsoft.Web.Administration apparently being able to read IISExpress application info, but information was very scarce, and the one I found on my system wouldn't even let me instantiate ServerManager without admin privileges.

这篇关于如何确定哪些IIS Express实例正在使用端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆