首页 > 解决方案 > Nancy 503 错误切换到 OWIN 自主机

问题描述

我目前有一些由 Nancy.Hosting.Self 托管的网络服务

我需要将服务从 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);
        }
    }
}

其他注意事项:

标签: authenticationowinnancyself-hostingtopshelf

解决方案


我在这里找到了答案

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

运行以下解决了该问题:

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

推荐阅读