【问题标题】:Both HTTP and HTTPS endpoints on Service Fabric Asp.Net Core stateless ServiceService Fabric Asp.Net Core 无状态服务上的 HTTP 和 HTTPS 端点
【发布时间】:2017-03-29 04:23:59
【问题描述】:

我有一个 Service Fabric Stateless Asp.Net Core 服务。

该服务使用 HTTP 端点。 我需要支持 HTTPS 端点以及 HTTP

到目前为止我的步骤:

  1. 已将证书添加到 Azure KeyVault
  2. 将 ApplicationManifest.xml 更新为:

    <Policies>
      <EndpointBindingPolicy EndpointRef="ServiceEndpointHttps" CertificateRef="Certificate" />
    </Policies>
    

最后

<Certificates>
    <EndpointCertificate X509StoreName="MY" X509FindValue="XXXX" Name="Certificate" />
  </Certificates>
  1. 在 ServiceManifest.xml 中添加了端点 443 端口

我现在只需要启用 HTTPS 端点。 在 Program.cs 我有这个:

  public static void Main(string[] args)
        {
            ServiceRuntime.RegisterServiceAsync("MyType", context => new WebHostingService(context, "ServiceEndpoint")).GetAwaiter().GetResult();

            Thread.Sleep(Timeout.Infinite);
        }
 internal sealed class WebHostingService : StatelessService, ICommunicationListener
        {
            private readonly string _endpointName;

            private IWebHost _webHost;

            public WebHostingService(StatelessServiceContext serviceContext, string endpointName)
                : base(serviceContext)
            {
                _endpointName = endpointName;
            }

            #region StatelessService

            protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
            {
                return new[] { new ServiceInstanceListener(_ => this) };
            }

            #endregion StatelessService

            #region ICommunicationListener

            void ICommunicationListener.Abort()
            {
                _webHost?.Dispose();
            }

            Task ICommunicationListener.CloseAsync(CancellationToken cancellationToken)
            {
                _webHost?.Dispose();

                return Task.FromResult(true);
            }

            Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
            {
                var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName);

                string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}";

                _webHost = new WebHostBuilder().UseKestrel()
                                               .UseContentRoot(Directory.GetCurrentDirectory())
                                               .UseStartup<Startup>()
                                               .UseUrls(serverUrl)
                                               .Build();

                _webHost.Start();

                return Task.FromResult(serverUrl);
            }

            #endregion ICommunicationListener
        }

如何在此处注册 HTTPS 端点?

添加第二个 ServiceRuntime.RegisterServiceAsync 不起作用。

另外,启用它后,我如何(从门户或 powershell)将 KeyVault 中的证书安装到已部署和运行的虚拟机规模集?

【问题讨论】:

    标签: azure-service-fabric


    【解决方案1】:

    您实际上可以使用 Kestrel + HTTPS + Service Fabric,但这更像是一次冒险。

    1) 您需要自己查找证书(见下文) 2)如果您在本地机器上测试,则需要将证书添加到 LocalMachine 并确保您授予 NETWORK SERVICE 权限(mmc \ Certificate \ Local Machine \ Personal \ Cert,右键单击 All-tasks \ Manage Private Keys ...)

            private X509Certificate2 FindCertificate(string thumbprint)
            {
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                try
                {
                    store.Open(OpenFlags.ReadOnly);
                    X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindByThumbprint,
                        thumbprint, false); // Don't validate certs, since the test root isn't installed.
                    if (col == null || col.Count == 0)
                        return null;
                    return col[0];
                }
                finally
                {
                    store.Close();
                }
            }
    
            Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
            {
                var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName);
    
                string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}";
    
                _webHost = new WebHostBuilder().UseKestrel(options =>
                                                {
                                                    options.UseHttps(FindCertificate("<thumbprint-remove-magic-character at the beginning if copy&paste from MMC>"));
                                                })
    

    【讨论】:

      【解决方案2】:

      我刚刚在周末遇到了这个问题。我遵循了一些建议,不使用 Kestrel,而是使用 WebListener:

      new WebHostBuilder()
      .UseWebListener()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseStartup<Startup>()
      .UseUrls($"{endpoint.Protocol}://+:{endpoint.Port}")
      .Build();
      

      还要注意使用 + 作为 url 而不是 FabricRuntime.GetNodeContext().IPAddressOrFQDN

      希望这会有所帮助。

      【讨论】:

      • 谢谢,为什么建议使用 WebListener Vs Kestrel?对于物联网应用程序或每天点击数十亿次的 API,哪个更好?我在代码中使用了 + 但应用程序仍然只检测 HTTP 端点,我需要更改代码以发送它们,但我不知道该怎么做,所有在线指南都是关于 OWIN 而不是 Asp.net核心。
      • 有一篇关于在SF中使用ASP.NET Core的好文章,但是很难找到。它涵盖了何时应该使用 WebListener 与 Kestrel 以及如何使用。 docs.microsoft.com/en-us/azure/service-fabric/… 和这篇第三方文章显示了 SSL 配置 dzimchuk.net/…
      【解决方案3】:

      我使用 Azure 应用程序网关解决了问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-18
        • 2020-02-12
        • 2016-11-16
        • 2017-09-27
        • 2016-08-27
        • 1970-01-01
        • 2017-03-07
        • 2018-04-09
        相关资源
        最近更新 更多