确定运行Visual Studio Web App的端口
时间:2020-03-06 14:44:49 来源:igfitidea点击:
我一直在使用此博客条目中的宏将Visual Studio调试器添加到我正在处理的Web应用程序的已运行实例上。但是,如果我有多个运行的Visual Studio Web服务器实例,则很幸运,它将添加到该实例上。
有没有一种方法可以确定在Web项目中配置了哪个端口,以便我可以修改宏以过滤对该进程的选择?
进一步的信息
我知道我们可以将端口号设置为静态端口,而我只是从一开始就这样做,我想确定的是如何以编程方式确定已定义的端口号,以便我可以修改宏(在链接的博客条目中),并确保它连接到Visual Studio Web服务器的正确实例。
我运行事物的方式是,我有两个(或者多个)Visual Studio实例运行,每个实例包含一个解决方案,其中包含一个Web项目和一个或者多个其他项目,通常是一个Installer项目),所以当我触发宏时从给定的Visual Studio实例中,我想在该实例加载的解决方案中找到Web项目,并确定它所针对的端口。
解决方案
如果转到Web项目的属性并在" Web"选项卡下查看,则可以指定项目始终在哪个端口上启动。然后,我们可以单击"启用编辑并继续",这样就不必停止调试并连续重新启动。
此代码将为我们提供当前解决方案中项目的所有端口的列表:
Sub GetWebProjectPorts() Dim ports As String For Each prj As Project In DTE.Solution.Projects For Each p As EnvDTE.Property In prj.Properties If p.Name.Contains("DevelopmentServerPort") Then If Not String.IsNullOrEmpty(ports) Then ports += "," End If ports += p.Value.ToString() End If Next Next MsgBox(ports) End Sub
如果那可以,我已经找到了可能实际上与该技巧有关的链接。
但是,它需要DllImport调用和很多乐趣。
我们可以在此处查看该文章:http://bytes.com/forum/thread574901.html
来自实际网站的报价:
By calling into iphlpapi.dll using PInvoke interop. Google around for GetExtendedTcpTable and iphlpapi.dll, I'm sure you will find some existing stuff. Willy.
最后一个:
Here are some API-Methods for my purposes: GetTcpTable() AllocateAndGetTcpExTableFromStack() GetExtendedTcpTable() I wrote a small programm that is able to show me all Processes with the belonging TCP-Ports (with AllocateAndGetTcpExTableFromStack()) running under Windows XP. Now my problem is, that under Windows 2000 I can't use AllocateAndGetTcpExTableFromStack() or GetExtendedTcpTable() so I'm only able to use GetTcpTable() to list all TCP-Ports but without the belonging processes. Did somebody have the same problem or is there another way (.Net or WMI etc.) to solve my problem? Thanks in advance, Werner