In today’s cloud-first, mobile-first technology world, most Store apps have at least some integration with web services or other devices on a network. These include apps that fetch online weather content, news or sports scores, media or podcast downloaders and even peer-to-peer games, chat or VoIP apps. These apps are built using a wide variety of networking APIs that are a part of the Universal Windows Platform.
In Windows 10, our networking team has invested significant effort in improving these APIs in terms of their speed and memory performance as well as the capabilities and flexibility they provide to you, the developer. In this blog post, we’ll discuss some of the key improvements we have made in this area, and illustrate how to leverage these in your own apps.
Networking APIs in Universal Windows Platform
The Universal Windows Platform (UWP) contains a large set of networking APIs that are targeted at diverse scenarios – from querying the connectivity status of the device and connecting to peer devices, to communicating with REST web services and downloading large media files in the background. In this blog post, we’ll focus on the following subset of networking APIs:
- Windows.Networking.Sockets– Typically used in P2P chat, VoIP and VPN apps
- System.Net.Sockets– .NET version of Sockets API widely used in cross-platform libraries and apps
- WinSock– Widely used in cross-platform libraries such as OpenSSL or multiplayer game libraries
- Windows.Web.Http and System.Net.Http– Used to communicate with REST web services
- Windows.Networking.BackgroundTransfer– Used for uploading or downloading data even when app is not in the foreground
- Windows.Data.Json– Used to serialize/deserialize JSON content served by webservices
These APIs represent different layers in networking from Sockets at the lowest layer, HTTP building on top of it and finally, Background Transfer and JSON that add more value building on top of HTTP.
New in Windows 10
In Windows 10, the above-mentioned APIs have been enhanced for greater performance and for providing more flexibility to developers. We discussed all of these improvements in detail in this video for BUILD 2015.
- Socket Broker: Allows apps to listen on Sockets even when the app is not in the foreground
- Batched Sends: A new optimization in the Sockets API implementation can make sending data up to 4x faster.
- HTTP/2: HTTP APIs now support the new, faster version of HTTP protocol for UWP apps. This makes REST webservice downloads and uploads up to 3x faster.
- Post-processing in Background Transfer: UWP apps can now handle the completion of their background downloads/uploads in a background task even before the app is launched the next time.
Due to time constraints, we couldn’t cover all of the new features we’re adding to Windows 10 in the video. Here are a few more:
System.Net.Sockets and Winsock for UWP Apps
With Windows 10, System.Net.Sockets and Winsock have been added into the API surface for UWP app developers. These were both highly requested APIs for Windows Store apps (they were already available for Windows Phone Silverlight apps). WinSock is also supported for Windows 8.1 apps with the latest updates to Visual Studio 2014.
The current API surface of both Winsock and System.Net.Sockets for UWP apps is based on that of Phone 8.1 Silverlight and continues to support most of the types, properties and methods (some APIs that are considered obsolete have been removed). For more details on System.Net.Sockets, see this post on the .NET blog.
Client certificate support for StreamSocket class
Windows.Networking.StreamSocket class supports using SSL/TLS to authenticate the server the app is talking to. In certain cases, the app also needs to authenticate itself to the server using a TLS client certificate. Starting in Windows 10, you can provide a client certificate on the StreamSocket.Control object (this must be set before the TLS handshake is started). If the server requests the client certificate, Windows will respond with the certificate provided.
Here is a code snippet showing how to implement this:
var socket = new StreamSocket(); Windows.Security.Cryptography.Certificates.Certificate
certificate = await GetClientCert();
socket.Control.ClientCertificate = certificate; await socket.ConnectAsync(destination,
SocketProtectionLevel.Tls12);
Handling null values and better debugging experience for JSON
One of the key developer requests we had for our Windows.Data.Json API in Windows 8.1 was to enable better handling of null values. Starting with Windows 10, we have added new, intuitive APIs to serialize and deserialize null values to and from JSON strings. Here is an example of how you can handle null value in JSON strings in Windows 10 UWP apps:
Serializing an object to JSON (Stringify):
jsonObject[key] = JsonValue.CreateNullValue(); var outputString = jsonObject.Stringify();
Deserializing a JSON string (Parse):
IJsonValue myJsonValue = jsonObject.GetNamedValue(key); if (myJsonValue.ValueType == JsonValueType.Null) { Phone = null; } else { var myValue = myJsonValue.GetString(); }
The new JsonValueType.Null enumeration value enables handling of null values with the same programming patterns that developers use today for integers, strings or Boolean values.
Richer debugging experience in Visual Studio for JSON
The next addition to Windows.Data.Json API is an improved debugging experience with Visual Studio. In Windows 8.1, developers stepping through their code while debugging would only see the type of a JsonObject or JsonArray instance but not its contents. This makes it harder for developers to debug their parsing logic and ensure that their objects have been correctly populated. Starting in Windows 10, you can now inspect the internal contents of JsonObject and JsonArray types. Here’s an example with a JsonArray object:
Improved SD card support for Background Transfer
In Windows 8.1, the Windows.Networking.BackgroundTransfer API supported downloading data on a removable storage (e.g. SD Card). The design was to download the content in the internal temporary cached storage first and then move the file to the destination. As a result, the size of the file which can be downloaded was constrained by the amount of internal storage available.
In Windows 10, this implementation has been changed to check whether the download is happening in a different drive than where the application is installed and use that location for the temporary cache. This will alleviate the problem of not being able to download large content (content which is greater in size than the available internal storage) on SD card.
Related Content
The entire set of networking APIs in UWP is too large to cover here. You can watch the following videos from Build 2015 for more info on other APIs in the networking area:
- .NET Networking APIs for UWP – http://aka.ms/dotnetuwp
- Windows.Devices.AllJoyn – https://channel9.msdn.com/Events/Build/2015/2-623
- Wi-Fi and Network Connectivity APIs – https://channel9.msdn.com/Events/Build/2015/2-86
- Wi-Fi Direct APIs – https://channel9.msdn.com/Events/Build/2015/3-98
- Hotspot 2.0 API – https://channel9.msdn.com/Events/Build/2015/2-80
- Bluetooth apps – https://channel9.msdn.com/Events/Build/2015/3-739
- IoT & Azure integration – https://channel9.msdn.com/Events/Build/2015/2-67
Looking Ahead
With Windows 10, we have evolved our networking APIs from Windows 8.1 to provide better performance and capabilities to you, the developer. Further, since these APIs are a part of UWP, you can use them to build apps for the entire range of Windows devices. With upcoming releases, we will continue to improve the performance of our networking APIs as well as add features that enable developers to build awesome, differentiated experiences on Windows. We look forward to hearing your feedback on which networking APIs are currently missing from UWP and are highest priority for you to deliver your app on Windows. You can file an idea on UserVoice or join the Windows Insiders program and submit feedback through the forums.