Svd*_*ner 3 authentication self-hosting topshelf nancy 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);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
其他注意事项:
Svd*_*ner 12
我在这里找到了答案
本质上,OWIN自托管不需要Nancy自托管所需的urlacl,如果不删除,实际上会导致503错误。(显然 OWIN 使用其他一些机制来获得端口的权限——这可能是 OWIN 需要管理员权限才能运行 .exe 或在 Visual Studio 中调试 .exe 的原因)
运行以下解决了问题:
netsh http delete urlacl url=https://+:2020/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
943 次 |
| 最近记录: |