gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:18:44 24 4
gpt4 key购买 nike

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

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

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 Core 实现与“经典”Web 应用程序相同的结果?

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

http://mywebsite.com/app1

http://mywebsite.com/app2

非常感谢!

最佳答案

正如@Vaclav所说,有必要通过UseWebListener更改UseKestrel。

但问题是WebListener绑定(bind)的地址不同。

查看此帖子以了解更多详细信息 https://github.com/aspnet/Hosting/issues/749

有必要在 serverUrl 上使用 + 代替 localhost 或其他计算机名称。

因此,将模板代码更改为:

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


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

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

_webHost.Start();

return Task.FromResult(serverUrl);
}

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

string serverUrl = $"{endpoint.Protocol}://+:{endpoint.Port}/service1";

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

_webHost.Start();

return Task.FromResult(serverUrl);
}

效果非常好。

关于azure - 如何使用在群集上共享同一端口的子路径在 Azure Service Fabric 上部署 Asp.Net Core 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38226157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com