Using RequestManager in ASP.NET Core

A few weeks ago, I published an article about managing HTTP request in ASP.NET Core on the StreamWriter website. Driven by the feedback that I got, I decided to create a library that would provide an easy way to send requests to multiple APIs without having to think about the performance of the operations every time.

The framework is, of course, not bound to ASP.NET Core, and can be used in any .NET Core project. However, there are some features that become more powerful in the context of ASP.NET, so I will this article to that.

You can find the StreamWriter RequestSender library on Github, or you can download it from NuGet (as we will do in this tutorial); it can be used from any .NET Core project.

What does RequestManager do?

RequestManager handles the requests by reusing the same HttpClient object when the requests are sent to the same API. For multiple APIs, the HttpClient objects will be saved and reused in the proper situation.

The library can send GET, POST, PUT, PATCH and DELETE requests with JSON, XML or form-data content types, and receive any type of response, with the possibility to translate it from JSON and XML.

Setting up

For this tutorial, I created a new ASP.NET Core WebAPI project. If you want to learn more about creating and deploying this type of applications, you can take a look at my article. If you prefer using the CLI, you can find an article treating that subject here.

Installing the package

If you are using Visual Studio, you can run the following command in the Package Manager Console:

Install-Package StreamWriter.RequestManager -Version 1.0.0

or from the CLI:

dotnet add package StreamWriter.RequestManager — version 1.0.0

If you prefer the GUI, you can right click on the project in the Solution Explorer, select Manage NuGet Packages and search for RequestManager in the browse tab:

Setting up the models & controllers

In order to simplify this tutorial, I will create two controllers that will act as two different applications: one will represent our web app, that contains the RequestManager, while the other will act as an external API that gets called through the RequestSender class.

As for the model (which will be shared by the two controllers), I will create a simple Person class:

For the “external API” controller, I will just set up two actions, using GET and POST, respectively, which will perform the very simple operation of returning a Person object:

Sending simple requests

To demonstrate the functionality of RequestManager, I will create another controller, called SandboxController, that will send requests to the external API.

I created two different actions in this controller too, so we can easily test the different endpoints.

The controller contains a RequestSender reference, which can be found in the SWriter.RequestManager package. The object gets instantiated in the constructor, with the URI pointing to the base of the other controller.

The requests are then sent through the GetAsync and PostAsync methods, which return a Response object. This can be translated into a model with the ResultAs method, by providing your model class as the generic type and the content type that the endpoint returns as the parameter (currently, JSON, XML and forms are supported).

Sending and reading different formats

As I mentioned, you can send data in three formats: JSON, XML and form-data. To do so, simply add a parameter to the PostAsync method (or PutAsync & PatchAsync):

The content type that needs to be read can also be changed by providing the right parameter to the ResultAs method.

Useful links

Repository with this article’s example

RequestManager repository

RequestManager NuGet package

StreamWriter webiste (for feedback)