In the first part of this article series, we have learned how to set up our development environment only using Docker + VS Code. In this second part, we will code a few lines.
First, we need to install some packages to our project. Open the terminal in the “app” project folder and enter the following command:
These packages are needed to communicate with Redis service.
Now, we need to change our code in order to use Redis. Open the Startup.cs file and add the following code inside the ConfigureServices method:
We need to set up a ConnectionMultiplexer aiming Redis to our data protection strategy. Second, we define that the app will use Redis cache. The last part is just adding Session and setting some options. The last thing we have to do in this file is to add app.UseSession(); inside the Configure method.
In order to test if our app is running, let’s set up a docker-compose file to start a Redis.
Create a docker-compose.yml file in the root folder of our app:
Then, write in this file the following code:
After that, we need to change our devcontainer.json in order not to use our dockerfile anymore, but use the docker-compose file instead:
After changing the file, don’t forget to save it.
Now, the next step is to rebuild our environment. Click in the remote development icon:
And choose “Remote-Containers: Rebuild Container”.
The objective here is to set our container development environment up again.
Wait for a while, and you will be able to access the terminal again:
Enter in the app folder and type dotnet run:
And the console will show our app running:
Is our app running? Open the browser and go to http://localhost:5000 (it will redirect to 5001 in https probably).
The nice part is that our session is stored in Redis! Open the browser at http://localhost:8081 to check Redis Commander (an online tool to manage Redis):
So far, so good! In the next part of this article series, we will run more than one replica of our app to see how they share the same session trough Redis.
About the author
Luiz Adolphs is a Software Engineer at Poatek.