As part of the recent update to WinUX, new networking APIs have been included which leverage the power of the Windows HttpClient with a framework which make it easy to extend but also keep it simple enough to use for UWP applications.
If you’ve not yet looked at the WinUX project, you can explore the code on GitHub or get using it via our NuGet packages!
What’s in the box?
Here is what is currently available:
WindowsNetworkManager
The WindowsNetworkManager is what it says on the tin. It’s a manager for your network processes, which we’ll discuss shortly.
The way the network manager works comes in two flavors.
Flavor one allows you to register your network process with the manager through a queue based system with a priority. The network manager will then pull processes of the queue and run them every minute or so. If you’re performing a GET request, for example, you’ll need to declare a callback action in your process so the manager can complete the end-to-end for you. This is great for a fire-and-forget scenario, particularly when you might be synchronizing data from your app to a service.
The other flavor is immediate execute where you provide your network process for execution and you get the response as part of the method call. This is great for when you can’t wait to get the information.
JSON network processes
As standard, we supply 5 network processes which use JSON as part of the request or response. These include GET, POST, PUT, PATCH and DELETE. These processes take advantage of the WinUX JSON SerializationService so you never have to worry about passing in or getting out a JSON string, we’ll always provide you with the typed object you’re expecting.
How to use the WindowsNetworkManager
This section will cover the various ways that you can use the WindowsNetworkManager.
Creating and executing a network process
Creating a network process couldn’t be easier. You can choose from one of the available JSON processes or create one of your own, particularly useful if you work with XML rather than JSON.
You can create your network process as follows:
There are some additional properties that you can set on your network process including the headers that you’d like to send up as part of the request.
Now that you have your process ready, it’s time to execute it. Now with this, you can do it in two ways. You can either directly call the ProcessRequestAsync method on your process, or you can pass it into the WindowsNetworkManager queue like this:
If you’re going to be using the queue mechanism, don’t forget to start the WindowsNetworkManager:
We recommend initializing and starting the WindowsNetworkManager from the OnLaunched method in your App.xaml.cs, but you can do it at any time if you need to!
FAQ
Q. I’m developing a regular .NET application and want this! How do I get it?
Lucky for you, we bundled up a version that uses the System variant of the HttpClient for you to use. It works in exactly the same way and has a set of processes for JSON too. It’s called the NetworkManager and yes, this does also work with UWP and Xamarin applications. Go wild!
Q. You mentioned processes could be extended. How do I do that?
We use a base class for network processes simply named NetworkProcess. This is an abstract class which has a set of properties and methods that are generally required for a process to be executed. All you need to do is add your execution logic and we’ll handle the rest.