System.Net.HttpListenerException: 在前缀'http://localhost:8080'上侦听失败。

27

我正在运行来自Scott Allen的ASP.Net基础课程的以下代码

using System;
using Microsoft.Owin.Hosting;
using Owin;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string uri = "http://localhost:8080";
            using (WebApp.Start<Startup>(uri))
            {
                Console.WriteLine("Started!");
                Console.ReadKey();
                Console.WriteLine("Stopping!");
            }
        }
    }

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseWelcomePage();
            //app.Run(
            //  ctx => ctx.Response.WriteAsync("Hello Owin!"));
        }
    }
}

然而,当我运行控制台应用时,我会收到一条消息。
    Unhandled Exception: System.Reflection.TargetInvocationException: Exception has
been thrown by the target of an invocation. ---> System.Net.HttpListenerExceptio
n: Failed to listen on prefix 'http://localhost:8080/' because it conflicts with
 an existing registration on the machine.
   at System.Net.HttpListener.AddAllPrefixes()
   at System.Net.HttpListener.Start()
   at Microsoft.Owin.Host.HttpListener.OwinHttpListener.Start(HttpListener liste
ner, Func`2 appFunc, IList`1 addresses, IDictionary`2 capabilities, Func`2 logge
rFactory)
   at Microsoft.Owin.Host.HttpListener.OwinServerFactory.Create(Func`2 app, IDic
tionary`2 properties)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
 Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
t[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuild
er builder)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(StartContext conte
xt)
   at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
   at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions opt
ions)
   at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider service
s, StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
   at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url)
   at ConsoleApplication1.Program.Main(String[] args) in e:\EShared\Dev2015\WebA
ppScottAllen\ConsoleApplication1\ConsoleApplication1\Program.cs:line 12
Press any key to continue . . .

我从任务管理器的性能选项卡运行资源监视器,可以看到8080上有2个监听端口条目。
两个都具有图像=系统,PID=4,IPv6未指定,协议TCP,防火墙状态不允许,未受限制。
我对监听端口不够熟悉,如何让代码运行?

我在想是否需要关闭端口。 https://dev59.com/X2oy5IYBdhLWcg3wPLn0 我需要一个好的关于端口和套接字的参考资料来学习。 - Kirsten
端口和TCP流量 https://www.youtube.com/watch?v=AXrFCbD4-fU - Kirsten
4个回答

50

当遇到以下错误时: "Failed to listen on prefix 'http://someURL:somePortNo/' because it conflicts with an existing registration on the machine." 有时并不一定是因为有应用程序正在监听该端口- 因此,Netstat -abno 的输出并不总是有帮助的。如果已经注册的应用程序在运行,则可以通过查看Netstat提供的信息来缩小引起问题的应用程序。

然而,即使相关应用程序停止了,您仍会收到此错误,因为该错误指示有一个注册过程。因此,正确的诊断命令是:

netsh http show urlacl

我们需要检查输出并确认列出的保留URL中是否已配置以侦听应用程序尝试使用的特定端口。您需要注意该特定应用程序的“Reserved URL”字段的值。稍后您需要它来删除导致错误的注册。

卸载该特定应用程序 - 假设他们的卸载过程确实包括取消注册 - 可能会解决问题。或者,您可以采用更直接和精确的方法,使用命令删除URL预留:

(请注意,如果冲突是合法的,则最好重新配置应用程序以侦听不同的端口。)

netsh http delete urlacl url=<value of "Reserved URL" in the output of netsh http show urlacl>
当命令起作用时,您将看到输出:URL reservation successfully deleted
再次运行netsh http show urlacl,您现在会发现该url注册确实已经删除。 现在运行您的应用程序不应导致之前看到的错误。

我尝试了这个,但是出现了这个错误<<在此时是意外的。 - Erhan Urun
你能截图你输入的内容并在这里发布吗?我认为问题之一是由于某些格式限制,上面显示的文本会显示额外的一对尖括号,在url=后面,你应该提供保留URL的值(你可以尝试在它周围加上单引号/双引号)。你可能正在输入你不应该输入的尖括号。 - shivesh suman
我还进行了编辑,现在示例中只显示一个尖括号。请注意,在示例中使用尖括号通常表示所需参数的占位符,并且通常在提供值时不使用任何尖括号。以这种方式呈现命令行参数的占位符值是很常见的。 - shivesh suman

1

我曾经遇到过同样的问题,解决方法很简单。我同时打开了其他控制台应用程序,它们使用了相同的端口号。当我关闭所有控制台应用程序后,我就能够运行程序并且不再出现这个错误。


0

我通过卸载几个程序成功解决了问题。不幸的是,我没有在每个程序卸载后进行测试,所以我不知道是哪一个程序引起了问题。

这些程序包括Dropbox、Goto Assist、Goto Meeting和一个winforms应用程序。


我应该先阅读这篇文章 https://dev59.com/nXVD5IYBdhLWcg3wOo5h?rq=1 以使用 netstat -a -b - Kirsten

0

我在Visual Studio中遇到了同样的错误,它友好地告诉我哪个端口是错误的。然后我在管理员命令提示符中运行了这个命令:

netsh http delete urlacl url=http://+:44308/

注意:记得加上最后的斜杠。否则会出现错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接