当前位置 : 主页 > 网络编程 > ASP >

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

来源:互联网 收集:自由互联 发布时间:2021-06-24
Service Fabric样本如wordcount,web应用程序侦听子路径中的端口,如下所示: http://localhost:8081/wordcount 此配置的代码是:(请参阅GitHub https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/blob/m
Service Fabric样本如wordcount,web应用程序侦听子路径中的端口,如下所示:

http://localhost:8081/wordcount

此配置的代码是:(请参阅GitHub https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/blob/master/Services/WordCount/WordCount.WebService/WordCountWebService.cs上的文件)

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            return new[]
            {
                new ServiceInstanceListener(initParams => new OwinCommunicationListener("wordcount", new Startup(), initParams))
            };
        }

使用此配置,我们可以使用相同的端口在同一群集上部署其他Web应用程序(8081)

http://localhost:8081/wordcount

http://localhost:8081/app1

http://localhost:8081/app2

等等.

但Asp.Net Core项目模板不同,我不知道如何在侦听器配置上添加子路径.

下面的代码是我们在项目模板(Program.cs类WebHostingService)中的代码:

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

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);
            }

语义有点不同,但都在同一点上结束.
问题是,即使我在serverUrl的末尾添加子路径它也不起作用,Web应用程序总是响应根http://localhost:8081/

看看我在下面的代码段中尝试过的方式:

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

如何使用asp.net核心获得与“经典”Web应用程序相同的结果?

目标是在端口80上的azure上发布,以便让用户获得更好的体验,例如:

http://mywebsite.com/app1

http://mywebsite.com/app2

非常感谢!

Kestrel不支持多个应用程序之间的URL前缀或端口共享.你必须使用 WebListener:

使用Microsoft.AspNetCore.Hosting…_webHost = new WebHostBuilder().UseWebListener()

网友评论