authentication - 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);
}
}
}
其他注意事项:
- UrlAcls 和 SslCerts 已正确设置为适用于该端口,正如它与 NancySelfHost 一起使用所证明的那样。
- 将 NancyFx 与 Owin 一起使用时,我没有按照503 错误的重复 urlacl 条目
- 我尝试过高于 :5000 的端口,但没有帮助
- 以管理员身份通过 Visual Studio 运行或以管理员身份从控制台运行时会出现同样的问题。(令人讨厌的是,OWIN 似乎需要管理员权限才能自行托管)
- 503 是在运行任何处理程序代码之前生成的。(IOW,webservice 代码入口处的断点未命中。)
解决方案
我在这里找到了答案
从本质上讲,OWIN 自托管不需要 Nancy 自托管所需的 urlacl,如果不删除它实际上会导致 503 错误。(显然 OWIN 使用其他一些机制来获取端口权限——这可能是 OWIN 需要管理员权限才能运行 .exe 或在 Visual Studio 中调试 .exe 的原因)
运行以下解决了该问题:
netsh http delete urlacl url=https://+:2020/
推荐阅读
- react-native - react-native:无法使用发布模式访问Android权限
- azure - 获取 无法解析请求。(错误代码:InvalidRequestFormat)在 PowerShell 上的模块更新-AzureRmVmss
- php - php - 如何通过多个类获取和发送href链接
- c++ - Eclipse C++ - 如何抑制特定行的警告?
- javascript - 使用 js 和原型处理 Ajax 调用返回的困难
- javascript - 如何使用 Violentmonkey 脚本跳过此倒计时
- django - Django-为任何模型数据的任何更改创建日志,即创建、更新和删除?
- php - php DOMDocument:元素在另一个中结束
- sparql - Wikidata SPARQL 多重过滤器
- image - Github 图片的链接已下载而不是显示