Azure service fabric 服务结构Asp.Net核心无状态服务上的HTTP和HTTPS端点

Azure service fabric 服务结构Asp.Net核心无状态服务上的HTTP和HTTPS端点,azure-service-fabric,Azure Service Fabric,我有一个服务结构无状态Asp.Net核心服务 该服务使用HTTP端点。 我需要支持HTTPS端点和HTTP 到目前为止,我的步骤是: 已将证书添加到Azure KeyVault 已使用以下内容更新ApplicationManifest.xml: <Policies> <EndpointBindingPolicy EndpointRef="ServiceEndpointHttps" CertificateRef="Certificate" /> </Policie

我有一个服务结构无状态Asp.Net核心服务

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

到目前为止,我的步骤是:

  • 已将证书添加到Azure KeyVault
  • 已使用以下内容更新ApplicationManifest.xml:

    <Policies>
      <EndpointBindingPolicy EndpointRef="ServiceEndpointHttps" CertificateRef="Certificate" />
    </Policies>
    
    
    
  • 最后呢

    <Certificates>
        <EndpointCertificate X509StoreName="MY" X509FindValue="XXXX" Name="Certificate" />
      </Certificates>
    
    
    
  • 在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
            }
    
    publicstaticvoidmain(字符串[]args)
    {
    ServiceRuntime.RegisterServiceAsync(“MyType”,context=>NewWebHostingService(context,“ServiceEndpoint”)).GetAwaiter().GetResult();
    Thread.Sleep(Timeout.Infinite);
    }
    内部密封类WebHostingService:无状态服务,ICommunicationListener
    {
    私有只读字符串_endpointName;
    专用IWebHost(网络主机);;
    公共WebHostingService(无状态serviceContext serviceContext,字符串endpointName)
    :base(serviceContext)
    {
    _endpointName=endpointName;
    }
    #区域无状态服务
    受保护的重写IEnumerable CreateServiceInstanceListeners()
    {
    返回new[]{new ServiceInstanceListener(=>this)};
    }
    #端域无状态服务
    #区域ICommunicationListener
    void ICommunicationListener.Abort()
    {
    _webHost?.Dispose();
    }
    任务ICommunicationListener.CloseAsync(CancellationToken CancellationToken)
    {
    _webHost?.Dispose();
    返回Task.FromResult(true);
    }
    任务ICommunicationListener.OpenAsync(CancellationToken CancellationToken)
    {
    var endpoint=FabriRuntime.GetActivationContext().GetEndpoint(_endpointName);
    字符串serverUrl=$“{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}”;
    _webHost=新建WebHostBuilder().UseKestrel()
    .UseContentRoot(目录.GetCurrentDirectory())
    .UseStartup()
    .useURL(服务器URL)
    .Build();
    _webHost.Start();
    返回Task.FromResult(服务器URL);
    }
    #端域ICommunicationListener
    }
    
    如何在此处注册HTTPS端点

    添加第二个ServiceRuntime.RegisterServiceAsync无效


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

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

    new WebHostBuilder()
    .UseWebListener()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup<Startup>()
    .UseUrls($"{endpoint.Protocol}://+:{endpoint.Port}")
    .Build();
    
    newwebhostbuilder()
    .UseWebListener()
    .UseContentRoot(目录.GetCurrentDirectory())
    .UseStartup()
    .useURL($“{endpoint.Protocol}://+:{endpoint.Port}”)
    .Build();
    
    还请注意,url使用+而不是
    FabriRuntime.GetNodeContext().IPAddressOrFQDN


    希望这有帮助。

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

    new WebHostBuilder()
    .UseWebListener()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseStartup<Startup>()
    .UseUrls($"{endpoint.Protocol}://+:{endpoint.Port}")
    .Build();
    
    newwebhostbuilder()
    .UseWebListener()
    .UseContentRoot(目录.GetCurrentDirectory())
    .UseStartup()
    .useURL($“{endpoint.Protocol}://+:{endpoint.Port}”)
    .Build();
    
    还请注意,url使用+而不是
    FabriRuntime.GetNodeContext().IPAddressOrFQDN


    希望这能有所帮助。

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

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

    你实际上可以使用Kestrel+HTTPS+服务结构,但这更像是一次冒险

    1) 您需要自己找到证书(见下文) 2) 如果在本地计算机上进行测试,则需要将证书添加到本地计算机,并确保授予网络服务权限(mmc\certificate\local machine\Personal\Cert,右键单击所有任务\管理私钥…)

    private X509Certificate2 FindCertificate(字符串指纹)
    {
    X509Store=新的X509Store(StoreName.My,StoreLocation.LocalMachine);
    尝试
    {
    打开(OpenFlags.ReadOnly);
    X509Certificate2Collection col=store.Certificates.Find(X509FindType.FindByThumbprint,
    指纹,false);//由于未安装测试根目录,因此不验证证书。
    如果(列==null | |列计数==0)
    返回null;
    返回列[0];
    }
    最后
    {
    store.Close();
    }
    }
    任务ICommunicationListener.OpenAsync(CancellationToken CancellationToken)
    {
    var endpoint=FabriRuntime.GetActivationContext().GetEndpoint(_endpointName);
    字符串serverUrl=$“{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}”;
    _webHost=新建WebHostBuilder()。UseKestrel(选项=>
    {
    选项。使用HTTPS(FindCertificate(“”);
    })
    
    您实际上可以使用Kestrel+HTTPS+服务结构,但这更像是一次冒险

    1) 您需要自己找到证书(见下文) 2) 如果在本地计算机上进行测试,则需要将证书添加到本地计算机,并确保授予网络服务权限(mmc\certificate\local machine\Personal\Cert,Right-clic)