title: Enable Https for Asp.Net WEB API 2 Self-Host
body: <p>It costs me 2 days already to make my web service support Https, the interesting thing is I could not find any official document on how to implement this common requirement.  </p>
  
  <p>by referring some posts, this is what I did:</p>
  
  <ul>
  <li><p>Applied a Http certificate from authority, and import to Windows key store.</p>
  
  <p>Now I got 2 script commands like:</p></li>
  </ul>
  
  <blockquote>
    <p>netsh http add urlacl url=<a href="http://+:9999/" rel="nofollow">http://+:9999/</a> user=Everyone</p>
    
    <p>netsh http add sslcert ipport=0.0.0.0:9999 certhash=XXXXXXXXXXXXXXXXXXXX appid={12345678-db90-4b66-8b01-88f7af2e36bf}</p>
  </blockquote>
  
  <ul>
  <li><p>modify the code:</p>
  
  <pre><code>string baseAddress = "https://+:9999/";
  try
  {
      // Start OWIN host 
      using (WebApp.Start&lt;SelfHostStartup&gt;(url: baseAddress))
      {
           //.....
  </code></pre></li>
  <li><p>Run that 2 script commands first, looks fine.   Run my application, error pop up:</p>
  
  <pre><code>System.Reflection.TargetInvocationException:
  Exception has been thrown by the target of an invocation. ---&gt; System.Net.HttpLi
  stenerException: Failed to listen on prefix 'https://+:9999/' because it conflic
  ts 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 WayneCloud.Program.Main(String[] args)
  </code></pre></li>
  </ul>
  
  <p>any idea?</p>
  
  <p>[edit0]:
  by using <code>netstat -an</code>, I could not see port <code>9999</code> on list.</p>
  
  <p>[edit1]:
  by using <code>netsh http delete urlacl url=http://+:9999/</code>, the exception was gone, now the <code>9999</code> was on listening in <code>netstat -an</code>, I tried using IE to access a https url, it give me a <code>500</code> error.</p>