We’ve finally finished screwing around with basic groundwork. Today we start writing actually useful application code! First, can we connect to Redis? Now we get to start writing “real” application code! Redis is an important piece of the Request Metrics puzzle. We should start using it sooner rather than later. Connecting to Redis implies some connection strings, so we’ll create a basic environment based configuration while we’re thinking about it.
Connecting To Redis From .NET Core
Redis clients are relatively complex compared to other service clients. They are expected to handle a number of cases that the server might handle in other data stores. This complexity means we’ll use a third party package instead of rolling our own.
StackExchange.Redis is the de facto official .NET Redis client and works with both .NET Core and “classic” .NET. After the package is added through Nuget, we write the minimum code necessary to prove our connection is working and Redis is up:
Super Basic, Environment Based Configuration File
The Redis address changes between environments which means the configuration should change for each environment. We could use appsettings.json (The .NET Core replacement for app.config and web.config), but it is a pain to deal with.
Instead we use a standalone, JSON configuration file which is loaded based on an environment variable. Our configuration doesn’t have much (we only have Redis so far!):
NOTE: Don’t forget to set the config file’s “Copy to Output Directory” property within Visual Studio!
The configuration file is loaded by a simple class:
The above code is a little gross, but you’ll never need to look at it again. The payoff is that fetching a configuration value is easy:
Now we’ve proven that our .NET Core app can connect to Redis. Our “not invented here” tendencies were soothed by a super basic environment configuration.
Next, we’ll set the environment variable on our dev server using Ansible to tell our application which environment it’s in. Also, we’ve neglected testing up till now and we need to find out how unit testing is done in .NET Core.
Request Metrics is application monitoring for the front-end. Understand your real
users, and how they experience your application. Fix the bugs and problems that get
in the way of your users. Respond to outages and attacks quickly with Request Metrics.
Get visibility into your web app in minutes instead of months.