Skip to content

redis-developer/redis-starter-csharp

Repository files navigation

This is a Redis starter template for C#/ASP.NET using:

Requirements

Getting started

Copy and edit the .env file:

cp .env.example .env

Your .env file should contain the connection string you copied from Redis Cloud.

Your .env.docker file will look similar to .env, but should use the appropriate docker internal URLs. Here is an example:

REDIS_URL="redis://redis:6379"

Next, spin up docker containers:

docker compose up -d

You should have a server running on http://localhost:<port> where the port is set in your .env file (default is 8080). You can test the following routes:

  1. GET /api/todos - Gets all todos
  2. GET /api/todos/:id - Gets a todo by ID
  3. GET /api/todos/search?[name=<name>]&[status=<status>] - Search for todos by name and/or status
  4. POST /api/todos - Create a todo with { "name": "Sample todo" }
  5. PATCH /api/todos/:id - Update todo by ID with { "status": "todo|in progress|complete" }
  6. DELETE /api/todos/:id - Delete a todo by ID

Additional runtime configuration:

  • LOG_LEVEL - ASP.NET log level name such as Information, Debug, or Error
  • LOG_STREAM_KEY - Redis stream name used for structured app/request logs (default logs)

Running tests

There are tests in the RedisStarterCSharp.Api.Tests project that can be run with the following command:

dotnet test

The tests setup and teardown their own todo data and index state. They expect a reachable Redis instance, using REDIS_URL when set and localhost:6379 otherwise.

Running locally outside docker

To run the development server outside of docker, first restore the solution:

dotnet restore

then run:

dotnet run --project RedisStarterCSharp.Api

Other Scripts

Formatting:

dotnet format

Dependency management is centralized in Directory.Packages.props, and the SDK is pinned in global.json.

Connecting to Redis Cloud

If you don't yet have a database setup in Redis Cloud get started here for free.

To connect to a Redis Cloud database, log into the console and find the following:

  1. The public endpoint (looks like redis-#####.c###.us-east-1-#.ec2.redns.redis-cloud.com:#####)
  2. Your username (default is the default username, otherwise find the one you setup)
  3. Your password (either setup through Data Access Control, or available in the Security section of the database page.

Combine the above values into a connection string and put it in your .env and .env.docker accordingly. It should look something like the following:

REDIS_URL="redis://default:<password>@redis-#####.c###.us-west-2-#.ec2.redns.redis-cloud.com:#####"

Run the tests to verify that you are connected properly.

Learn more

To learn more about Redis, take a look at the following resources:

Releases

No releases published

Packages

 
 
 

Contributors