尝试在 UWP 应用程序中使用 HttpListener 时出现异常

Exception when trying to use HttpListener in UWP app

我正在尝试构建 UWP 并实例化 HttpListener。但是,我在 Visual Studio 的错误列表中收到以下错误:Cannot find type System.Net.WebSockets.WebSocketContext in module System.Net.WebSockets.dll,阻止我构建和 运行 应用程序。

我的应用程序如下所示:

using System.Net;
using Windows.UI.Xaml.Controls;

namespace HttpTestServer
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
        }

        private static void MainPage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var httpListener = new HttpListener();
            httpListener.Prefixes.Add("http://*:9080/");
            httpListener.Start();
        }
    }
}

我认为这与我定位的最低 Windows 版本有关。我被迫以 Windows 10 周年更新版本 10.0.14393.0 为目标。我如何知道 HttpListener 是否包含在此版本的 Windows 中?根据 https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistener?view=netframework-4.8 它应该包含在 .NET Standard 2.0 中。

是否可以通过某种方式实例化 HttpListener

How do I know if HttpListener is included in this version of Windows?

来源于.Net Standard 2.0中包含的官方文档HttpListener,支持.Net Standard 2.0的最低UWP版本为16299(Fall Creators Update),因此您需要编辑应用程序的目标最小值版本到 16299.