ibe*_*dev 23 base-url asp.net-core-mvc asp.net-core
如何在没有请求的情况下获取AspNet核心应用程序中的基本URL?
我知道从请求你可以获得方案和主机(即$"{Request.Scheme}//{Request.Host}"会给出像https:// localhost:5000这样的东西),但是有可能从其他地方获取这些信息吗?
换句话说,如果我有一个需要构建绝对URL的服务类,当没有可用的http请求时,如何获取当前URL?
更新:也许这种情况甚至没有意义,因为托管URL完全在应用程序外部,这就是为什么只从请求主机中提取它才有意义.
小智 14
我出于某种原因需要在Start.cs Configure中获取基本URL,所以我想出了这个
var URLS = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
你是对的,托管URL是一个外部信息,你可以简单地将它作为配置参数传递给你的应用程序.
也许这会以某种方式帮助你:没有请求,你可以http://+:5000使用IWebHostBuilder接口获得配置的监听地址(如).它通过以下GetSetting方法提供对主机设置的访问:
/// <summary>
/// Get the setting value from the configuration.
/// </summary>
/// <param name="key">The key of the setting to look up.</param>
/// <returns>The value the setting currently contains.</returns>
string GetSetting(string key);
Run Code Online (Sandbox Code Playgroud)
有一个WebHostDefaults.ServerUrlsKey设置名称,允许配置监听地址.我们在添加.UseUrls扩展方法时覆盖它:
public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params string[] urls);
Run Code Online (Sandbox Code Playgroud)
或者urls如文档中所述定义配置参数(默认情况下,监听配置为localhost:5000).
因此,有了实例IWebHostBuilder,你可以调用.GetSetting(WebHostDefaults.ServerUrlsKey)并获取当前值.
,ASP.NET Core Module 生成动态端口分配给后端进程。CreateDefaultBuilder 调用 UseIISIntegration 方法。UseIISIntegration 将 Kestrel 配置为侦听 localhost IP 地址 (127.0.0.1) 的动态端口。如果动态端口是 1234,Kestrel 会在 127.0.0.1:1234 侦听。此配置替换了由提供的其他 URL 配置。
对于 IIS 集成,如果您在 WebHostBuilder.Build() 运行后获得地址,它就可以工作。
var builder = CreateWebHostBuilder(args);
var webHost = builder.Build();
var addresses = webHost.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
var address = addresses.FirstOrDefault();
AppDomain.CurrentDomain.SetData("BaseUrl", address ?? "");
webHost.Run();
Run Code Online (Sandbox Code Playgroud)
并在 HostedService 中获取本地 Kestrel 地址,如下所示:
string baseUrl = AppDomain.CurrentDomain.GetData("BaseUrl").ToString();
Run Code Online (Sandbox Code Playgroud)
但是有一个问题——这个地址没有用,因为你不能直接在这个地址上发出请求。IIS 集成中间件检查是否只有 IIS 处理程序可以对该地址发出请求。它会产生类似的错误:
<category>Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware</category>
<state>'MS-ASPNETCORE-TOKEN' does not match the expected pairing token 'ed5bc610-b7b9-4c1c-9941-954d0579edfc', request rejected.</state>
Run Code Online (Sandbox Code Playgroud)
在一般情况下(没有 IIS 集成),如果您使用配置为使用自定义端口(不是 5000)或动态端口 0 运行的 Kestrel,则这种获取地址的方法不起作用。在这种情况下,需要在一种延迟方式,只有在应用程序启动之后。
对于这种情况,我尝试了这种方式:在 StartUp 类的 Configure 方法中,我保存在私有成员中的 ServerAddressFeature 中。
private IServerAddressesFeature _serverAddressesFeature;
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
_serverAddressesFeature = app.ServerFeatures.Get<IServerAddressesFeature>();
... not related code here ...
Run Code Online (Sandbox Code Playgroud)
在 ConfigureServices 方法中,我添加了一个依赖项
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServerAddressesFeature>((sp) => _serverAddressesFeature);
... not related code here ...
Run Code Online (Sandbox Code Playgroud)
然后在托管服务中,我使用依赖注入获取此保存的功能,并使用它来获取地址。 它有效,只在 StartAsync 方法中获取地址,而不是在服务构造函数中!
public class WarmUpService : IHostedService
{
private readonly ILogger _logger;
private readonly IServerAddressesFeature _saf;
public WarmUpService(ILogger<WarmUpService> logger, IServerAddressesFeature serverAddressesFeature)
{
_logger = logger;
_saf = serverAddressesFeature;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
try
{
// the URL can be Got here
string baseUrl = _saf?.Addresses?.FirstOrDefault();
// await _WarmUp(baseUrl);
}
catch(Exception ex)
{
_logger.LogCritical(ex, "WarmUp Failed");
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17381 次 |
| 最近记录: |