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

时间:2017-04-29 02:31:18

标签: service asp.net-core azure-service-fabric

我在Azure VM上运行VS2017,Windows 2016使用SDK当前服务结构(截至(4/29/17),单节点)。在挖掘之前尝试简单地创建环境验证应用程序。使用VS工具创建ASP.NET Core Web应用程序。开箱即用,它可以构建,部署和运行良好。多个端点的此示例失败:https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-service-manifest-resources 我能找到的只是代码片段;什么都不适用于本地群集。 有人可以指向一个工作示例,它是一个具有http和https端点的本地群集上的ASP.NET Core Web应用程序。谢谢!

1 个答案:

答案 0 :(得分:3)

尝试简要介绍如何在使用SF5.5&amp ;;的ASP.NET核心项目中使用http和https在本地计算机上工作。 SDK2.5(3/2017)。我显示代码和配置,并列出所有页面作为信息来源的参考。我不可能一个人做这件事。感谢社区!
第一步:
为要使用的本地SF群集创建证书。我喜欢这个article来创建证书。然后,我使用certmgr提取证书指纹以获取证书详细信息 提示:从证书存储区复制指纹时,它将无效,我将其复制到记事本和Visual Studio代码,然后将其粘贴到配置中。 TChaing的此引用here表示存在隐藏字符。我只是输入(没有空格)。我知道证书有问题,因为事件查看器有错误消息,证明无法解析证书,这导致我得到了这个答案。
第二步:
对于我的应用程序和服务清单,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>

ServiceManifest文件   ...

  <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#代码。创建下面显示的Listeners有两种不同的方法。注释代码直接命名侦听器,例如“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;
    }

我希望这能为你带来所有缺失的部分。