13

我正在将应用程序从 .NET 4 移植到 .NET Core,但找不到 HttpListener 类的模拟

Error   CS0246  The type or namespace name 'HttpListener' could not be found (are you missing a using directive or an assembly reference?)  

更新1

        private readonly HttpListener _httpListener;

            if (!HttpListener.IsSupported)
        {
            throw new NotSupportedException(
                "The Http Server cannot run on this operating system.");
        }

        _httpListener = new HttpListener();
        _httpListener.Prefixes.Add(prefix);
        _sessionSettings = settings;
4

2 回答 2

21

如评论中所述,WebListener(在Microsoft.Net.Http.ServerNuGet 包中)是最接近的替代品,但具有不同的 API。或者,还有 Kestrel HTTP 服务器,它最好从 ASP.NET Core 堆栈中使用,但可以单独使用(但这很难设置)。

如果您要移植,我建议等到 .NET Core 2.0,它具有兼容的 APIHttpListener,可以跨平台工作,并且不需要您完全更改代码。

于 2017-05-13T15:39:38.287 回答
9

在 .NET Core 2.0 中我们没有这个问题(感谢 Martin Ullrich),所以现在我们需要安装Visual Studio Preview 2017 版本 15.3,我们可以在其中使用 .NET Core 2.0。

但默认情况下(目前最低)没有 .NET Core 2.0,我们需要在 VS 2017 安装后安装它。

PS: - 再次感谢 Martin Ullrich - 太棒了,仅在 5 月 10 日(在我询问前 3 天)宣布了 .NET Core 2.0 - 我现在有了

于 2017-05-14T14:15:02.033 回答