C# 服务结构:在基于Kestrel的无状态服务中支持HTTP和HTTPS
我有一个现有的无状态ASP.Net核心MVC服务结构服务,默认情况下(由于代码由向导生成)使用HTTP。我们还需要在相同的服务中使用相同的控制器支持HTTPS ServiceManifest.xml中定义了两个端点:C# 服务结构:在基于Kestrel的无状态服务中支持HTTP和HTTPS,c#,stateless,kestrel,C#,Stateless,Kestrel,我有一个现有的无状态ASP.Net核心MVC服务结构服务,默认情况下(由于代码由向导生成)使用HTTP。我们还需要在相同的服务中使用相同的控制器支持HTTPS ServiceManifest.xml中定义了两个端点: <Endpoints> <Endpoint Protocol="http" Name="HttpEndpoint" Type="Input" Port="16080" /> <Endpoint Protocol="https" Name="Ht
<Endpoints>
<Endpoint Protocol="http" Name="HttpEndpoint" Type="Input" Port="16080" />
<Endpoint Protocol="https" Name="HttpsEndpoint" Type="Input" Port="16443" />
</Endpoints>
以下是来自无状态服务派生类的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();
}))
};
受保护的重写IEnumerable CreateServiceInstanceListeners()
{
返回新服务InstanceListener[]
{
新ServiceInstanceListener(serviceContext=>
新的WebListenerCommunicationListener(serviceContext,“HttpEndpoint”,url=>
{
ServiceEventSource.Current.ServiceMessage(serviceContext,$“在{url}上启动WebListener”);
返回新的WebHostBuilder()
UseKestrel先生(
选项=>
{
options.NoDelay=true;
options.UseConnectionLogging();
})
.ConfigureServices(服务=>services.AddSingleton(服务上下文))
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup()
.useURL(url)
.Build();
}))
,
新ServiceInstanceListener(serviceContext=>
新的WebListenerCommunicationListener(serviceContext,“HttpsEndpoint”,url=>
{
ServiceEventSource.Current.ServiceMessage(serviceContext,$“在{url}上启动WebListener”);
返回新的WebHostBuilder()
UseKestrel先生(
选项=>
{
options.NoDelay=true;
使用https(“mycert.pfx”,“这个不是密码”);
options.UseConnectionLogging();
})
.ConfigureServices(服务=>services.AddSingleton(服务上下文))
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup()
.useURL(url)
.Build();
}))
};
运行时没有错误,服务确实绑定到端口,但我在尝试连接时被拒绝,没有解释原因
Windows防火墙已禁用
我知道这是代码(或框架/产品)的问题,因为如果我注释掉其中一个监听器,另一个监听器在其各自的端口上工作。但是当两个监听器都被实例化时,两个监听器都不工作。看起来它们以某种方式相互干扰,我不知道它们的容量是多少
我尝试过从UseKestrel()切换到UseWebListener(),并观察到了相同的行为。因此,这不是Kestrel特有的问题
我尝试让它通过不在端点中指定端口来选择端口,并观察到了相同的行为
简言之,我被困住了