如何在ASP.NET CORE 2.1的ConfigureServices方法中获取主机和端口值

3

我需要从 public void ConfigureServices(IServiceCollection services)动态获取主机(包括协议)和端口的值。我试图通过IServerAddressesFeature来实现,但是当我尝试解决它时,IoC返回了null(到目前为止还没有注册)。那么,是否有其他选项可以获取这些信息?


在你的Startup ConfigureServices(IServiceCollection)中,为什么需要使用Web主机的Scheme、Host和Port? - smn.tino
1
@smn.tino在注册第三方服务时需要传递信息。有什么想法吗? - managerger
好的,我猜想您想知道这些信息将用于什么目的。 - smn.tino
@smn.tino不知道它能在多大程度上帮助找到答案,但据我所知,它是基于信息构建链接的。有什么想法吗? - managerger
2个回答

2
ConfigureServices(IServiceCollection services) 方法旨在配置应用程序的服务,如 Microsoft 文档 中所述。
在这里,似乎不能直接从 WebHost 中获取方案、主机和端口信息。 实际上,通常是在 Program.cs 中创建 WebHost
var webHost = WebHost.CreateDefaultBuilder(args) // initialise web host
            .UseStartup<Startup>()
            .Build();

然而,您可以通过查看CreateDefaultBuilder()使用的配置来检索WebHost使用的方案、主机和端口。

实际上,查看WebHost代码,您可以看到CreateDefaultBuilder()appsettings.jsonappsettings.Development.json中加载配置以配置WebHost,例如方案、主机和端口。您可以在ConfigureServices(IServiceCollection services)中查看相同的配置数据,并推断出相同的配置信息。

如果您的目标是Asp.Net Core 2.1,则可以直接在appsettings.json文件中配置您的终结点。您可以通过查看Startup.cs中的Configuration属性,在ConfigureServices(IServiceCollection services)中执行相同操作。

您的Startup.cs可能如下所示:

public class Startup {

    public Startup(IConfiguration configuration) {

        Configuration = configuration;

    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services) {

        // get http url
        var httpEndpoint = Configuration["Kestrel:Endpoints:Http:Url"];

        // get https url
        var httpsEndpoint = Configuration["Kestrel:Endpoints:Https:Url"];

        /*
         * Use IConfiguration to retrieve information
         * about loaded configuration.
        */

        // Add framework services.
        services.AddMvc();

    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();

    }

}

请查看IConfiguration Microsoft文档页面,了解更多读取配置数据的方法。

等一下,根据我的判断,httphttps变量仍将为空。 httpEndpointhttpsEndpoint。你是说还有另一种方法被首先调用,还是说需要设置一个硬编码的appsettings.json文件? - Tom Stickel
@TomStickel 这段代码片段是一个示例,演示了如何通过硬编码的 appsettings.json 文件检索 http/https url。文件结构与 MS 文档中描述的相同:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.1&tabs=aspnetcore2x - smn.tino
你可以注入 IServer 并通过它访问。请参阅 https://dev59.com/NVYO5IYBdhLWcg3wIuTp#60791568。 - James Durda

1

我需要在Kestrel中托管的Service Fabric无状态应用程序的ConfigureServices内访问端口。

一种方法(ref)是将要在Startup中执行的操作放入到构建WebHost的位置,即Programs.cs中(在我的情况下是Service Fabric StatelessService的CreateServiceInstanceListeners)

以下是我正在处理的代码片段:

                    var endpoint = Context.CodePackageActivationContext.GetEndpoint("ServiceEndpoint");
                    return new WebHostBuilder()
                        .UseKestrel(/* SSL STUFF GOES HERE */)
                        .ConfigureServices(
                            services => services
                                .AddSingleton<StatelessServiceContext>(serviceContext)
                                .AddMvc(options =>
                                {
                                    // Using the port here:
                                    options.SslPort = endpoint.Port;
                                    options.Filters.Add(new RequireHttpsAttribute());
                                }))                                   
                        .UseStartup<Startup>()
                        .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                        .UseUrls(url)
                        .Build();
                }))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接