【问题标题】:HttpSelfHostServer use prefixes like HttpListenerHttpSelfHostServer 使用像 HttpListener 这样的前缀
【发布时间】:2017-03-09 19:00:35
【问题描述】:

HttpListener 中有 Prefixes 字段。您可以添加一些字符串,例如“http://+:8080/”,您的应用程序只需处理对 8080 端口的所有查询。

我有HttpSelfHostServer,它只能使用有效的 Uri 字符串,例如输入HttpSelfHostConfiguration 类。我想在我的HttpSelfHostServer 中获得相同的行为,就像在HttpListener 中一样,前缀如下:

http://+:8080/

http://*:8080/

http://some.url:8080/

等等……

...
var configuration = new HttpSelfHostConfiguration(prefix)
var server = new HttpSelfHostServer(configuration);
...

【问题讨论】:

    标签: c# self-hosting httplistener self-host-webapi


    【解决方案1】:

    在这种情况下,localhost 被重写为+,因此localhost:80800.0.0.0:8080127.0.0.1:8080 在您想在所有接口上侦听的情况下都应该工作。

    如果您希望发生这种自动重写,HttpSelfHostConfiguration 的第二个参数采用 HostNameComparisonMode 可以设置到Exact

    【讨论】:

    • 所以,http://+:8080/ 等于 http://localhost:8080/http://*:8080/ 等于 http://0.0.0.0:8080/http://some.url:8080/ 等于 127.0.0.1:8080。对于 3 种情况中的每一种,我应该使用哪种 HostNameComparisonMode?我知道:ExactStrongWildcardWeakWildcard,但不知道在哪种情况下使用哪个?
    • 当我尝试使用http://+:8080/ 时出现错误Invalid URI: The hostname could not be parsed
    • @Max:这就是这个问题和答案的重点。您不能使用 http://+:8080,因为它不是有效的 URI,但您可以在我的答案中使用该解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多