I have some webservices currently hosted with Nancy.Hosting.Self
I need to move the services from Nancy.Hosting.Self to being hosted with Microsoft.Owin.SelfHost so that I can use OWIN for user authentication.
Theoretically, I should be able to simply replace my NancySelfHost class with an Owin Startup class. However, when run the service with my Owin Startup class, Nancy returns: "HTTP Error 503. The service is unavailable."
I am currently swapping the hosting class based on build parameters. (They are launched via TopShelf)
Launcher:
#define OWIN
using Topshelf;
namespace BasisRESTApi
{
public class Program
{
private static readonly string _serviceName = "MyRestApi";
private static readonly string _displayName = "My REST services";
private static readonly string _description = "Minor RESTful web services for interop.";
public static void Main()
{
HostFactory.Run(x =>
{
x.UseLinuxIfAvailable();
// Automate recovery
x.EnableServiceRecovery(recover =>
{
recover.RestartService(0);
});
#if OWIN
x.Service<Startup>(s =>
{
s.ConstructUsing(name => new Startup(_serviceName));
#else
x.Service<NancySelfHost>(s =>
{
s.ConstructUsing(name => new NancySelfHost());
#endif
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.StartAutomatically();
x.RunAsLocalSystem();
x.SetDescription(_description);
x.SetDisplayName(_displayName);
x.SetServiceName(_serviceName);
});
}
}
}
NancySelfHost: (Works)
using System;
using System.Configuration;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using Logging;
using Nancy.Hosting.Self;
using static Logging.Logging;
namespace BasisRESTApi
{
public class NancySelfHost
{
private NancyHost _nancyHost;
public void Start()
{
var hostUrl = "https://localhost:2020";
_nancyHost = new NancyHost(new Uri(hostUrl));
_nancyHost.Start();
}
public void Stop()
{
_nancyHost.Stop();
}
}
}
Owin Startup: (Runs but returns 503 Errors)
using Logging;
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
using System;
using System.Configuration;
using System.Net;
using System.Text.RegularExpressions;
using System.Web.Http;
using static Logging.Logging;
[assembly: OwinStartup(typeof(BasisRESTApi.Startup))]
namespace BasisRESTApi
{
public class Startup
{
public string ServiceName { get; set; }
private static IDisposable _application;
public Startup(string serviceName)
{
ServiceName = serviceName;
}
public void Start()
{
var hostUrl = "https://localhost:2020";
_application = WebApp.Start<Startup>(hostUrl);
}
public void Stop()
{
_application?.Dispose();
}
public void Configuration(IAppBuilder application)
{
UseWebApi(application);
application.UseErrorPage();
var listener = (HttpListener)application.Properties["System.Net.HttpListener"];
// Different authentication methods can be specified for the webserver here
listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
//NOTE:All of the above can be removed and the issue is not impacted.
application.UseNancy();
}
/// <summary>
/// Provide API Action
/// </summary>
/// <param name="application"></param>
private static void UseWebApi(IAppBuilder application)
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
application.UseWebApi(config);
}
}
}
Other notes:
- UrlAcls and SslCerts are properly set to work for this port, as evidenced by it working with NancySelfHost.
- I do not have duplicate urlacl entries as per 503 Error when using NancyFx with Owin
- I have tried ports higher than :5000 and it did not help
- The same issues occur when run through visual studio as an administrator, or run from the console as an administrator. (Annoyingly, OWIN seems to require admin rights to self-host)
- The 503 is generated prior to any of the handler code being run. (IOW, breakpoints at the entry of the webservice code are not hit.)