.NET Aspire integrations are a curated suite of NuGet packages specifically selected to facilitate the integration of cloud-native applications with prominent services and platforms, e.g. Redis, PostgreSQL, etc. Each integration furnishes essential cloud-native functionalities through either automatic provisioning or standardized configuration patterns.
.NET Aspire integrations come in two distinct flavors: hosting integrations, and client integrations. Hosting integrations are used to model and configure various resources in a .NET Aspire app, while client integrations are used to map configuration to various client libraries.
There is an ever growing list .NET Aspire integrations created and shipped by Microsoft and the community. .NET Aspire is flexible and anyone can create their own package to integrate with their own services.
Let's improve our application by adding an integration to it. We will add a integration that will help us to connect to a Redis cache to improve our API performance.
To add the Redis hosting integration to our App Host, we need to install the Aspire.Hosting.Redis
NuGet package. This package provides the necessary pieces to configure the service in the App Host. Redis is provided through a container image in this workshop, and when we start the .NET Aspire App Host, it will automatically download the Redis container image and start the Redis server.
With the NuGet package installed, we can add Redis to our App Host:
-
Open the
Program.cs
file in theAppHost
project. -
Add the following code under
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
Here, we have configured the Redis cache with the name
cache
. This name is used to identify the cache in theApi
orMyWeatherHub
. -
Update the
api
in the App Host with a reference to the cache.var api = builder.AddProject<Projects.Api>("api") .WithReference(cache);
-
Additionally, we could configure Redis Commander, a Redis management tool. As part of the
Aspire.Hosting.Redis
package, Redis Commander is available in the same integration. To add Redis Commander, update the code adding the Redis resource to call theWithRedisCommander()
method on the returned builder:var cache = builder.AddRedis("cache") .WithRedisCommander();
We haven't made any changes to the Api
or MyWeatherHub
projects, but we can see the Redis cache start when we start the App Host.
Important
Since Redis runs in a container you will need to ensure that Docker is running on your machine.
-
Ensure Docker Desktop or Podman is running.
-
Start the App Host project.
-
You will see both the Redis container and Redis Commander container download and start in both the dashboard and in Docker Desktop.
There are two types of caching that we could integrate into our ASP.NET Core applications:
- Output caching: A configurable, extensible caching method for storing entire HTTP responses for future requests.
- Distributed caching: A cache shared by multiple app servers that allows you to cache specific pieces of data. A distributed cache is typically maintained as an external service to the app servers that access it and can improve the performance and scalability of an ASP.NET Core app.
We will add the Output caching Redis client integration to our Api
project. This integration will help us to cache the response of our API in Redis cache.
-
Install the
Aspire.StackExchange.Redis.OutputCaching
NuGet package in theApi
project to get access to the Redis APIs. -
Open the
Program.cs
file in theApi
project. -
Add the following code under the
var builder = WebApplication.CreateBuilder(args);
at the top of the file:builder.AddRedisOutputCache("cache");
Note that we are using the "cache" name to reference the Redis cache that we configured in the App Host.
-
The
NwsManager
has already been configured to use Output caching, but with a memory cache. We will update it to use the Redis cache. Open theNwsManager.cs
file in theData
folder. -
In the
NwsManagerExtensions
class you will find aAddNwsManager
method. -
DELETE the following code:
// Add default output caching services.AddOutputCache(options => { options.AddBasePolicy(builder => builder.Cache()); });
Because we configured the application to use Redis cache in the
Program.cs
file, we no longer need to add the default output caching policy.
-
Start the App Host project and open the
MyWeatherHub
project from the dashboard. -
Click on a city and then click on it again. You will see that the response is cached and the second request is much faster than the first one under the
Traces
tab. -
You can also see the cached response in Redis Commander. Open Redis Commander by clicking on the
cache-commander
endpoint in the dashboard. Under stats you will see connections and commands processed. -
In addition, you can see logs for the Redis cache and Redis Commander in the
Console
tab.
.NET Aspire integrations are flexible and customizable. By default, the Redis integration uses a Redis container image from Docker Hub. However, you can use your own Redis container image by providing the image name and tag after the AddRedis
method. For example, if you have a custom Redis container image such as Garnet, you can provide the image registry, name, and tag in the App Host as follows:
var cache = builder.AddRedis("cache")
.WithImageRegistry("ghcr.io")
.WithImage("microsoft/garnet")
.WithImageTag("latest");
Note: there is a new Garnet integration that simplifies this integration, but the above highlights the flexibility of the integrations.
-
Run the application and you will now see Garnet running in the dashboard and in Docker Desktop.
-
You can also see the logs for Garnet in the
Console
tab.
In this section, we added a Redis hosting integration to the App Host and Redis output caching client integration in the API. We saw how the response was cached in the Redis cache and how the second and subsequent requests were much faster than the first one. We also saw how to use Redis Commander to manage the Redis cache.
There are many more Aspire integrations available that you can use to integrate with your services. You can find the list of available integrations in the .NET Aspire documentation.
A natural next step would be to integrate a database or leverage Azure Redis Cache as a hosted solution. Integrations for these and more are available on NuGet.
Next: Module #6: Deployment