Service Fabric 本地群集 ASP.NET Core https 终结点

拉里·奥特曼

我在 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Service Fabric Asp.Net Core无状态服务上的HTTP和HTTPS终结点

具有 2 个公开的 https 终结点和不同端口的 Service Fabric 群集

Service Fabric本地群集上的托管环境Asp.net Core

更新Azure Service Fabric群集中的自定义终结点

Team Services 部署到本地 Service Fabric,无需公开终结点

如何将null传递给asp.net core 3.1中的终结点

ASP.NET Core 2.0结合了Cookie和承载授权给同一终结点

不带终结点的MicroService的asp.net Core项目是什么

ASP.NET Core Openiddict引发“无法从此终结点返回OpenID Connect响应”

如何在ASP.NET Core中创建无限流终结点

ASP.NET Core 3.0使用属性路由更改默认终结点路由

如何使用共享群集上相同端口的子路径在Azure Service Fabric上部署Asp.Net Core应用

无法在Azure上的Linux容器中为.net Core Kestral服务器配置HTTPS终结点

Service Fabric(ASP.NET Core)中的Appsettings

如何在使用ASP.NET Core中间件运行状况检查创建的运行状况检查终结点上有选择地强制实施HTTPS?

Service Fabric本地群集设置问题

如何允许CORS用于ASP.NET WebForms终结点?

从Azure函数调用Asp.Net Web API终结点

无法信任ASP.NET Core中的本地HTTPs证书

将.Net Core应用程序部署到Linux Service Fabric群集

用于 asp.net 核心的 Service Fabric 中 docker 中的 HTTPS 不起作用

在Azure Service Fabric中的ASP.NET Core中使用WebListener

Azure Service Fabric中ASP.NET Core应用程序中的WebpackDevMiddleware

如何在Azure Service Fabric ASP.NET Core有状态服务中获取PartitionInfo?

Service Fabric Asp.net Core Kestrel HttpClient 以最小负载挂起

Service Fabric上的SignalR ASP.NET Core因HTTP 410 GONE而失败

Azure Service Fabric:直接在ASP.NET Core有状态服务中使用可靠的集合

创建Service Fabric Api服务仅允许ASP .NET Core 2.1

使用默认ASP.NET Core DI容器在Service Fabric上设置依赖项注入