重定向到另一个侦听器的服务侦听器

时间:2017-04-19 20:55:59

标签: azure-service-fabric

在我的服务架构应用程序中,我目前有一个在HTTPS上运行的Core Web服务。这工作正常,但我想这样做,以便如果通过HTTP访问Web服务,它将重定向到HTTPS版本。

我通过创建第二个服务侦听器来解决这个问题,该侦听器也在HTTP端点上加载Web项目,然后通过中间件重定向到HTTPS版本。这工作得相当好,我唯一的问题是,在加载项目时,它需要显然加载两个版本的Web项目,这需要一些时间。

理想情况下,可以通过某种方式在服务侦听器级别进行简单的重定向,我可以将其重定向到我的HTTPS侦听器,而无需启动Web应用程序的完整红场版本。

有什么想法?这就是目前的情况。

internal sealed class Web : StatelessService
    {
        public Web(StatelessServiceContext context)
            : base(context)
        { }

        /// <summary>
        /// Optional override to create listeners (like tcp, http) for this service instance.
        /// </summary>
        /// <returns>The collection of listeners.</returns>
        protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            return new ServiceInstanceListener[]
            {
                new ServiceInstanceListener(serviceContext =>
                    new WebListenerCommunicationListener(serviceContext, "ServiceEndpointSecure", url =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");

                        var context = FabricRuntime.GetActivationContext();
                        var config = context.GetConfigurationPackageObject("Config");
                        var environment = config.Settings.Sections["Environment"].Parameters["ASPNETCORE_ENVIRONMENT"].Value;

                        return new WebHostBuilder().UseWebListener()
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatelessServiceContext>(serviceContext))
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseEnvironment(environment)
                                    .UseUrls(url)
                                    .Build();
                    }),"HttpsListener"),
                 new ServiceInstanceListener(serviceContext =>
                    new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", url =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");

                        var context = FabricRuntime.GetActivationContext();
                        var config = context.GetConfigurationPackageObject("Config");
                        var environment = config.Settings.Sections["Environment"].Parameters["ASPNETCORE_ENVIRONMENT"].Value;

                        return new WebHostBuilder().UseWebListener()
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatelessServiceContext>(serviceContext))
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseEnvironment(environment)
                                    .UseUrls(url)
                                    .Build();
                    }),"HttpListener")
            };
        }
    }

在ServiceManifest.xml中

<Resources>
    <Endpoints>         
      <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="80" />
      <Endpoint Protocol="https" Name="ServiceEndpointSecure" Type="Input" Port="443" />
    </Endpoints>
  </Resources>

1 个答案:

答案 0 :(得分:2)

您只需从一个WebListener收听两个网址:

.UseUrls(&#34; http://+:80&#34;,&#34; https://+:443&#34;)

添加一些middleware以强制执行https。类似的东西:

 HttpRequest req = context.Request;
 if (req.IsHttps == false)
 {
     string url = "https://" + req.Host + req.Path + req.QueryString;
     context.Response.Redirect(url, permanent: true);
 }