23

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 . . .

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

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?

Kirsten Greed
  • 11,170
  • 26
  • 117
  • 234
  • I wonder if I need to close the port http://stackoverflow.com/questions/8688949/how-to-close-tcp-and-udp-ports-via-windows-command-line I need a good reference on ports and sockets to learn from – Kirsten Greed Apr 18 '15 at 20:08
  • Ports & TCP Traffic https://www.youtube.com/watch?v=AXrFCbD4-fU – Kirsten Greed Apr 18 '15 at 20:20

4 Answers4

43

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

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.

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:

(Note that if the conflict is legitimate, it may be better to reconfigure your application to listen on a different port instead.)

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

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

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.

shivesh suman
  • 1,251
  • 15
  • 23
  • I tried this but am getting this error << was unexpected at this time – Erhan Urun Jan 08 '20 at 08:43
  • Can you take a screenshot of what you typed and post here. I think one issue is that the text which shows up above, due to some formatting limitation shows extra pair of angle brackets, after url= you are supposed to provide the value of Reserved URL (you can try single/double quotes around it). You may be typing in the angle brackets which you are not supposed to type. – shivesh suman Jan 08 '20 at 20:19
  • I have also edited so that only one angle bracket shows up in the example now. Please note that use of angle brackets in an example usually represents a placeholder for a required parameters and typically when you would provide the value you would do it without any angle brackets. It is common to present placeholders values for command line arguments in this manner. – shivesh suman Jan 09 '20 at 20:18
1

I was able to solve the problem by uninstalling several programs. Unfortunately I did not test after each, so I don't know which one it was.

They included Dropbox, Goto Assist, Goto Meeting and a winforms application

Kirsten Greed
  • 11,170
  • 26
  • 117
  • 234
  • I should have read this first http://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on-windows?rq=1 to use netstat -a -b – Kirsten Greed Apr 18 '15 at 21:10
0

I had the same issue, and it was a silly fix. I had other console apps open that was using the same port number, so after I have closed all the console apps, I was able to run and did not get this error.

nihnih
  • 11
  • 1
0

I had the same error in Visual Studio which was kind enough to tell me which port was wrong. I then ran this command in an Administrator Command Prompt:

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

Note : It is important to remember to final slash. Otherwise you will get an error.

Rahbek
  • 1,061
  • 13
  • 9