Json Configuration

If you work with .NET Core the Json format is absolutely the best way because .NET Core offers a package that makes easy to bind the json file into a C# class but let's proceed in order, the json first:

{
	"Redis": {
		"Password": "my_super_secret_password",
		"AllowAdmin": true,
		"Ssl": false,
		"ConnectTimeout": 6000,
		"ConnectRetry": 2,
		"Database": 0,
		"ServiceName" : "my-sentinel", // In case you are using Sentinel
		"Hosts": [
		{
			"Host": "192.168.0.10",
			"Port": "6379"
		},
		{
			"Host": "192.168.0.11",
			"Port": "6381"
		}]
	},
	"MaxValueLength" = 1024,
	"PoolSize" = 5,
	"KeyPrefix" = "_my_key_prefix_",
}

Now:

Finally the dependency injection:

Example using Microsoft.Extensions.DependencyInjection:

Example using Castle.Windsor:

If you prefer to use the appsettings.json file, is enough to invoke the GetSection("Redis") line of the code

Last updated

Was this helpful?