我有一个现有的无状态 ASP.Net Core MVC 服务结构服务,默认情况下(由向导生成代码)使用 HTTP。我们还需要使用相同的控制器在相同的服务中支持 HTTPS。
ServiceManifest.xml 中定义了两个端点:
<Endpoints>
<Endpoint Protocol="http" Name="HttpEndpoint" Type="Input" Port="16080" />
<Endpoint Protocol="https" Name="HttpsEndpoint" Type="Input" Port="16443" />
</Endpoints>
下面是 StatelessService 派生类的 GetServiceInstanceListeners 覆盖:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "HttpEndpoint", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseKestrel(
options =>
{
options.NoDelay = true;
options.UseConnectionLogging();
})
.ConfigureServices(services => services.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
,
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "HttpsEndpoint", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseKestrel(
options =>
{
options.NoDelay = true;
options.UseHttps("mycert.pfx", "this_isnt_the_password");
options.UseConnectionLogging();
})
.ConfigureServices(services => services.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
};
运行时没有错误,服务确实绑定到端口,但是我在尝试连接时被拒绝连接,没有解释原因。
Windows 防火墙已禁用。
我知道这是代码(或框架/产品)的问题,因为如果我注释掉其中一个侦听器,另一个在其各自的端口上工作。但是当两者都被实例化时,两者都不起作用。似乎他们在某种程度上相互干扰,我不知道以什么身份。
我尝试从 UseKestrel() 切换到 UseWebListener() 并观察到相同的行为。所以这不是 Kestrel 特有的问题。
我尝试通过不在端点中指定端口来让它选择端口,并观察到相同的行为。
简而言之,我被卡住了。