🖋️
StackExchange Redis Extensions
  • Home
  • Setup
  • Dependency Injection
  • Configuration
    • C# Configuration
    • Json Configuration
    • XML Configuration
  • Serializers
    • Newtonsoft Json.Net
    • MsgPack
    • System.Text.Json
  • Packages
  • Usage
    • Add, retrieve and remove complex object
    • Replace an object
    • Work with multiple items
    • Custom serializer
  • ASP.NET Core
    • Expose redis information
  • Helpful link
  • License
  • Work with the code
    • Unit tests
  • Usages
Powered by GitBook
On this page

Was this helpful?

Dependency Injection

Register all the needed part into your DI container

Becoming a super hero is a fairly straight forward process and one step of this process is to use a dependency injection container.

All you need to do is this:

/// <summary>
/// Add StackExchange.Redis with its serialization provider.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="redisConfiguration">The redis configration.</param>
/// <typeparam name="T">The typof of serializer. <see cref="ISerializer" />.</typeparam>
public static IServiceCollection AddStackExchangeRedisExtensions<T>(this IServiceCollection services, RedisConfiguration redisConfiguration)
    where T : class, ISerializer, new()
{
    services.AddSingleton<IRedisCacheClient, RedisCacheClient>();
    services.AddSingleton<IRedisCacheConnectionPoolManager, RedisCacheConnectionPoolManager>();
    services.AddSingleton<ISerializer, T>();

    services.AddSingleton((provider) =>
    {
        return provider.GetRequiredService<IRedisCacheClient>().GetDbFromConfiguration();
    });

    services.AddSingleton(redisConfiguration);

    return services;
}

There are two important things to know in this code block:

PreviousSetupNextConfiguration

Last updated 5 years ago

Was this helpful?

Ho to retrieve an instance of the redis configuration ( the solution);

Choise the right serializer ( few options)

here
here