Nancy 503错误切换到OWIN自主机

问题描述 投票:0回答:1

我有一些目前由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 Startup :(运行但返回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服务代码入口处的断点未被命中。)
authentication owin nancy self-hosting topshelf
1个回答
0
投票

我发现了答案here

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

运行以下解决了该问题:

netsh http delete urlacl  url=https://+:2020/
© www.soinside.com 2019 - 2024. All rights reserved.