且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

System.Net.HttpListenerException:无法侦听前缀'http://localhost:8080

更新时间:2022-03-19 22:28:34

遇到错误时:"Failed to listen on prefix 'http://someURL:somePortNo/' because it conflicts with an existing registration on the machine."并没有必要在某个应用程序上积极侦听该端口-因此Netstat -abno的输出可能并不总是帮助.如果已注册的应用程序正在运行,则可以通过查看Netstat提供的信息来帮助您缩小引起问题的应用程序的范围.

When faced with error: "Failed to listen on prefix 'http://someURL:somePortNo/' because it conflicts with an existing registration on the machine." It is not really necessary that there is an application actively listening on that port - thus output of Netstat -abno may not always help. If the already registered application is running it can help you narrow down to which application is causing the issue by looking at the info Netstat provides.

但是,即使该应用程序已停止,您仍会收到此错误,因为该错误表明已注册.因此,正确的诊断命令是:

However, you will get this error even after the application in question is stopped since the error indicates a registration. The correct diagnostic command therefore is:

netsh http show urlacl

我们需要检查输出,并检查是否已配置任何列出的保留URL来侦听应用程序尝试使用的特定端口.您需要记下该特定应用程序的保留URL"字段的值.您稍后将需要它来删除引起错误的注册.

We need to examine the output and check whether any of the listed reserved URLs is configured to listen on the specific port your application is trying to use. You need to note the value of the "Reserved URL" field for that specific application. You will need it later for deleting the registration which is causing the error.

卸载该特定应用程序-假设其卸载过程确实包含注销,则可以解决此问题.另外,您可以采用一种更直接,更精确的方法,使用该命令删除URL保留:

Uninstalling that specific application - assuming their uninstall procedure does include an un-registration - may resolve the problem. Alternatively you could take a more direct and precise approach of using the command for deleting a URL reservation:

(请注意,如果冲突是合法的,***重新配置您的应用程序以监听其他端口.)

netsh http delete urlacl url=<value of "Reserved URL" in the output of netsh http show urlacl>

该命令运行时,您将看到输出:URL reservation successfully deleted.

When the command works you will see output: URL reservation successfully deleted.

再次运行netsh http show urlacl后,您现在将看到url注册确实消失了.现在,运行您的应用程序应该不会导致您之前看到的错误.

Upon running netsh http show urlacl a second time you will now see that the url registration is indeed gone. And now running your application should not result in the error you were seeing earlier.