System.Net.HttpListenerException:无法侦听前缀'http://localhost:8080 [英] System.Net.HttpListenerException: Failed to listen on prefix 'http://localhost:8080

查看:187
本文介绍了System.Net.HttpListenerException:无法侦听前缀'http://localhost:8080的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从斯科特·艾伦(Scott Allen)的ASP.Net基础课程中运行以下代码

I am running the following code from Scott Allen's ASP.Net Fundamentals course

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!"));
        }
    }
}

但是,当我运行控制台应用程序时,会收到一条消息

However when I run the console app I get a message

    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个条目.

I ran the Resource Monitor from the Task Manager Performance Tab and can see that there are 2 entries on Listening Ports for 8080.

都具有Image = System,PID = 4,未指定IPv6,协议TCP,防火墙状态

Both have Image=System, PID=4, IPv6 unspecified, Protocol TCP, Firewall Status Not allowed, not restricted

我是侦听端口的新手,如何使代码正常工作?

I am new to Listening Ports, how do I get the code working?

推荐答案

遇到错误时:"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.

这篇关于System.Net.HttpListenerException:无法侦听前缀'http://localhost:8080的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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