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:
I discovered the answer here
Essentially, the urlacl that is required for Nancy self-host is not needed for OWIN self hosting, and in fact causes the 503 errors if it is not deleted. (Apparently OWIN uses some other mechanism to get rights to the port -- probably the reason why OWIN requires admin rights to run the .exe or to debug the .exe in Visual Studio)
Running the following resolved the issue:
netsh http delete urlacl url=https://+:2020/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With