c# - 如何在 Kestrel 中配置端点?

标签 c# asp.net port kestrel-http-server

我正在学习如何在 ASP.NET Core 2 中工作,我遇到了一个相当烦人的问题。每当我运行我的应用程序时,Kestrel 服务器都会忽略我的端点配置,而是开始监听一个随机端口。不用说,这不是我所期望的行为。在挖掘服务器日志时,我还发现了这条消息:

Overriding endpoints defined in UseKestrel() because PreferHostingUrls is set to true. Binding to address(es) 'http://localhost:<some random port>' instead.

到目前为止,我无法找到有关此“PreferHostingUrls”设置或如何更改它的任何文档。有谁知道我如何配置 Kestrel 以监听指定端口而不是随机端口?

我的主机配置如下所示:

WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(config)
        .UseKestrel(
            options =>
            {
                options.Listen(IPAddress.Loopback, 50734);
                options.Listen(IPAddress.Loopback, 44321, listenOptions =>
                {
                    listenOptions.UseHttps("pidea-dev-certificate.pfx", "****************");
                });
            })
        .UseStartup<Startup>()
        .UseIISIntegration()
        .Build();

最佳答案

好的,原来 IISExpress 是这里的罪魁祸首。

出于某种原因,Visual Studio 2017 的默认构建配置在 IISExpress 服务器上启动我的应用程序,该服务器不监听我的端点配置。为了解决这个问题,我只需要切换到自定义运行配置即可。

总而言之,我只需要从这里切换:

enter image description here

为此:

enter image description here

(PIdea 是我项目的名称)

关于c# - 如何在 Kestrel 中配置端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46879048/

相关文章:

c# - ContainerVisual.Offset 和 Visual.VisualOffset 之间有什么区别?

c# - 如何在不显示与 Excel 互操作的保存对话框的情况下保存工作簿?

c# - 递归匿名函数的 StackOverflowException

c# - 访问匿名类型集合中的属性 - C#

android - Android 模拟器上的 Expo 无法运行,出现问题

c# - 在 C#9 中,仅初始化属性与只读属性有何不同?

c# - 动态创建具有多个筛选字段的新字段搜索组

c# - ASP.NET Core 3 无登录管理器注册方案

port - 以管理员权限运行 IIS Express

iphone - 如何通过 objective-c 在 iphone 上获取 tcp/udp 开放端口列表?