Nancy 503错误切换到OWIN自主机

时间:2018-09-17 21:03:03

标签: authentication owin nancy self-hosting topshelf

我目前通过Nancy.Hosting.Self托管了一些Web服务

我需要将服务从Nancy.Hosting.Self移到Microsoft.Owin.SelfHost托管,以便可以使用OWIN进行用户身份验证。

从理论上讲,我应该能够简单地用Owin Startup类替换我的NancySelfHost类。但是,当使用我的Owin Startup类运行该服务时,Nancy返回:“ HTTP错误503。该服务不可用。”

我目前正在根据构建参数交换托管类。 (它们是通过TopShelf启动的)

发射器:

 #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 :(工作)

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启动:(运行,但返回503错误)

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);
        }
    }
}

其他说明:

  • 已将UrlAcls和SslCerts正确设置为可用于此端口,这是与NancySelfHost一起使用所证明的。
  • 根据503 Error when using NancyFx with Owin,我没有重复的urlacl条目
  • 我尝试使用高于:5000的端口,但没有帮助
  • 以管理员身份通过Visual Studio运行或以管理员身份从控制台运行时,也会发生相同的问题。 (令人讨厌的是,OWIN似乎需要管理员权限才能进行自我托管)
  • 503是在运行任何处理程序代码之前生成的。 (IOW,未命中Web服务代码条目的断点。)

1 个答案:

答案 0 :(得分:0)

我找到了答案here

本质上,OWIN自托管不需要Nancy自托管所需的urlacl,实际上,如果不删除它,则会导致503错误。 (显然,OWIN使用其他机制来获取端口的权限,这可能是OWIN需要管理员权限才能运行.exe或在Visual Studio中调试.exe的原因)

运行以下命令可以解决问题:

netsh http delete urlacl  url=https://+:2020/