我在 Azure VM、Windows 2016 上运行 VS2017,使用 SDK 当前的本地 Service Fabric(截至 2017 年 4 月 29 日),单节点。尝试在深入研究之前简单地创建一个环境验证应用程序。使用 VS 工具创建一个 ASP.NET Core Web 应用程序。它开箱即用,可以很好地构建、部署和运行。这个多端点的例子失败了:https : //docs.microsoft.com/en-us/azure/service-fabric/service-fabric-service-manifest-resources我能找到的只是代码片段;在本地集群上没有任何作用。有人可以指点我一个工作示例,它是本地集群上的 ASP.NET Core Web 应用程序,同时具有 http 和 https 端点。谢谢!
试图简要介绍如何使用 SF5.5 和 SDK2.5 (3/2017) 在 ASP.NET Core 项目中的本地机器上同时使用 http 和 https。我展示了代码和配置,并列出了所有页面作为信息来源的参考。我不可能独自完成这件事。感谢社区!
Step1:
创建本地SF集群使用的证书。我喜欢这篇关于创建证书的文章。然后我使用 certmgr 提取证书指纹以获取证书详细信息。
提示:当您从证书存储中复制指纹时,它将不起作用,我将其复制到记事本和 Visual Studio Code,然后再将其粘贴到配置中。这个参考这里by TChaing 表示有隐藏字符。我只是输入了它(没有空格)。我知道证书有问题,因为事件查看器有错误消息表明无法解析证书,这导致我得到了这个答案。
Step2:
对于我的应用程序和服务清单,Matt Kotsenas 的文章https://matt.kotsenas.com/posts/https-in-service-fabric-web-api很棒。他还有一个优雅的解决方案来创建侦听器,只需稍作调整即可与我的代码清单中的 ASP.Net Core 一起使用。
代码:
ApplicationManifest 文件
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<!-- Add policies for service endpoints -->
<Policies>
<EndpointBindingPolicy EndpointRef="ServiceEndpointHttps" CertificateRef="MyCert" />
</Policies>
...
</DefaultServices>
<!-- Add the certificate. In production use parameter references to the thumbprints. -->
<Certificates>
<EndpointCertificate X509FindValue="6e6a28c083c1b8114c4e2279b37cf6d684668aad" Name="MyCert" />
</Certificates>
</ApplicationManifest>
服务清单文件...
<Resources>
<Endpoints>
<!-- This endpoint is used by the communication listener to obtain the port on which to
listen. Please note that if your service is partitioned, this port is shared with
replicas of different partitions that are placed in your code. -->
<Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8340" />
<Endpoint Protocol="https" Name="ServiceEndpointHttps" Type="Input" Port="8343" />
</Endpoints>
在这种情况下,来自我的服务类“Web1”的 C# 代码。有两种不同的方法可以创建如下所示的监听器。注释代码直接命名侦听器,例如“ServiceEndpoint”并注意“HttpListner1”。这是 ASP.NET Core 中的新功能。如果您有多个端点,则必须对其进行具体命名。如果未提供名称,则 ServiceEndpoint 不会自动成为名称。
未注释的代码使用提取来获取服务名称,然后创建侦听器。在这种情况下,我使用“端点”变量还根据需要为侦听器添加了显式名称。
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
//return new ServiceInstanceListener[]
//{
// new ServiceInstanceListener(serviceContext =>
// new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
// {
// ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
// return new WebHostBuilder().UseWebListener()
// .ConfigureServices(
// services => services
// .AddSingleton<StatelessServiceContext>(serviceContext))
// .UseContentRoot(Directory.GetCurrentDirectory())
// .UseStartup<Startup>()
// .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
// .UseUrls(url)
// .Build();
// }), "HttpListner1"),
// new ServiceInstanceListener(serviceContext =>
// new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttps", (url, listener) =>
// {
// ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
// return new WebHostBuilder().UseWebListener()
// .ConfigureServices(
// services => services
// .AddSingleton<StatelessServiceContext>(serviceContext))
// .UseContentRoot(Directory.GetCurrentDirectory())
// .UseStartup<Startup>()
// .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
// .UseUrls(url)
// .Build();
// }), "HttpsListner1")
//};
var endpoints = Context.CodePackageActivationContext.GetEndpoints()
.Where(endpoint => endpoint.Protocol == EndpointProtocol.Http || endpoint.Protocol == EndpointProtocol.Https)
.Select(endpoint => endpoint.Name);
var eps = endpoints.Select(endpoint =>
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, endpoint, (url, listener) =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder().UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}
), endpoint));
return eps;
}
我希望这为您带来了所有缺失的部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句