Curious case of IISExpress error "Failed to register URL" when working with OWINHost

when working with an application using a bunch of micro services , ran into this error with iisexpress

Failed to register URL "https://localhost:3001/" for site "Contoso.Web" application "/". Error descriptio
n: Cannot create a file when that file already exists. (0x800700b7)

 C:\Program Files\IIS Express>iisexpress /config:D:\PROJECTS\api\.vs\config\applicationhost.config /trace:error
Starting IIS Express ...
Initializing the W3 Server Started CTC = -1673430150
W3 Server initializing WinSock. CTC = -1673430134
W3 Server WinSock initialized. CTC = -1673430118
W3 Server ThreadPool initialized (ipm has signalled). CTC = -1673430118
Start listenerChannel http:0
Failed to register URL "https://localhost:3001/" for site "Contoso.Web" application "/". Error descriptio
n: Cannot create a file when that file already exists. (0x800700b7)
Failed to initialize site bindings
Error initializing ULATQ. hr = 800700b7
Terminating W3_SERVER object
InitComplete event signalled
Process Model Shutdown called
Unable to start iisexpress.

Cannot create a file when that file already exists.
For more information about the error, run iisexpress.exe with the tracing switch enabled (/trace:error).

So iisexpress is complaining that another process is already sing the port 3001. But I was sure that no other process is using the port 3001. I had checked using all the methods explaining in my own post how-to-fix-the-error-port-is-currently-used-by-another-application

But still IISExpress is not able to bind to the port 3001. I knew one thing  where this port was used by another microservices . We had a total of 4 services and one of them was running inside IIS and other 3 were running as a OWIN HOST inside different windows services (using TopShelf.)

When I tested my owin service on the same port,it is able to bind fine. You can see owin host starting code below

[code lang="csharp"]

WebApp.Start(hostAdress, appBuilder
{
//application initialization code
});

This code is using Microsoft.Owin.Hosting to start a OWIN web server in the hostaddress .in our case hostaddress was https://localhost:3001.

So it seems OWIN is able to bind but iisexpress is not able to . I tried many things

  • Run both IISExpress and OWIN Services under administrator
  • Added urlacl for https://localhost:3001

netsh http add urlacl url=https://localhost:3001/ user=Everyone

  • removed urlacl

netsh http delete urlacl https://+:3001/

None of this seems to fix the iisexpress error. Then it struck me,even though we started the OWINHost using WebApp.Start, in the code,it was never stopped. This process which OWINHost is running is exited but  the entry did not get removed from the http.sys.

 

So in order to stop an OWINHost,what you have to do is

[code lang="csharp"]

IDisposable webhost=WebApp.Start(hostAdress, appBuilder
{
//application initialization code
});

 

Now to stop the webhost,we have to call dispose.Since we had this code inside a windows service,dispose was called when an exception occured or the service is stopped

[code lang="csharp"]

webhost.Dispose();

Or rather the correct way to use OWinHost is using using block as explained here

So iisexpress was not able to start the service because OWIN service had it started but not stopped before the process exited.