UWP and .NET Standard 2.0–Remembering the ‘Forgotten’ APIs :-)

This post comes out of a conversation that I was having with my colleague Pete around the use of the HttpListener class inside of a UWP application.

I’m using HttpListener as an example because it’s the type that we were talking about but there’s many, many other types that I could use instead.

Over the past few years, I’ve been gradually building up a table of APIs in my head partitioned something like;

  • .NET APIs that I know are in the .NET Framework
  • .NET APIs that I know are available to the UWP developer

and so it’s become easier and easier over time to know that (e.g.) a class like HttpListener isn’t available to the UWP developer because that class wasn’t in the API set offered by .NET Core on which UWP apps are based.

Of course, in recent times .NET Standard 2.0 support has come to UWP apps running on Windows Fall Creators Update as detailed;

Announcing UWP Support for .NET Standard 2.0

and so this means that a UWP application that’s definitely running on Windows build 16299 upwards has access to .NET Standard 2.0 making our example API HttpListener suddenly usable inside of a UWP application.

Knowing whether an API is/isn’t in one of these variants of .NET can be tricky to keep in your head but the “.NET API Browser” web page provides a quick look up;

.NET API Browser

So, what does this mean? If I’m sitting in a UWP project that has been set to target 16299 upwards of the UWP platform;


and then I can spin up a little piece of a MainPage.xaml type UI;


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
         <Viewbox Margin="40">
                 <Run Text="Number of HTTP requests served "/>
                 <Run Text="{x:Bind NumberOfRequests, Mode=OneWay}"/>

and marry that up with a little code behind;

using System;
 using System.ComponentModel;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Runtime.CompilerServices;
 using Windows.UI.Xaml;
 using Windows.UI.Xaml.Controls;

namespace App1
     public sealed partial class MainPage : Page, INotifyPropertyChanged
         public event PropertyChangedEventHandler PropertyChanged;

        public MainPage()
             this.Loaded += OnLoaded;
         public int NumberOfRequests
             get => this.numberOfRequests;
                 if (this.numberOfRequests != value)
                     this.numberOfRequests = value;
         void FirePropertyChanged([CallerMemberName] string property = null)
                 () =>
                     this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
         async void OnLoaded(object sender, RoutedEventArgs e)
             // an endless async method isn't perhaps the nicest thing in the world
             // but it's very easy to read.
             this.listener = new HttpListener();

            while (true)
                 var context = await this.listener.GetContextAsync();

                if (context.Request.AcceptTypes.Contains(HTML))
                     // Ah, for an HtmlTextWriter being part of .NET Standard 2.0 😉
                     context.Response.ContentType = "text/html";
                     context.Response.StatusCode = 200;

                    using (var writer = new StreamWriter(context.Response.OutputStream))
                     context.Response.StatusCode = 501;
         static readonly string HTML = "text/html";
         HttpListener listener;
         int numberOfRequests;

and then make some changes to my application manifest;


and I’ve then got a little web server that I can hit from a second machine asking for //myMachine:8080/whatever and I can see responses being returned and the usage counter going up;


and that all seems to work quite nicely Smile

Again, the use of HttpListener here is just an example – I’m not sure whether/where you’d actually want to build a UWP app that offered up an HTTP server so it’s just an example of an API as it happened to be the one that we were discussing.

1 thought on “UWP and .NET Standard 2.0–Remembering the ‘Forgotten’ APIs :-)

  1. Pingback: A Follow-On Prague Experiment with Skeletons – Mike Taulty

Comments are closed.