还是直接上代码吧
public class AppOptions { public string RedisConnectionString { get; set; } = "localhost:6379"; }
services.Configure<AppOptions>(Configuration); services.AddSingleton<IConnectionMultiplexer,ConnectionMultiplexer>(sp => { var settings = sp.GetRequiredService<IOptions<AppOptions>>().Value; var configuration = ConfigurationOptions.Parse(settings.RedisConnectionString, true); return ConnectionMultiplexer.Connect(configuration); });
private readonly IDatabase _database; public AccountController(IConnectionMultiplexer connectionMultiplexer) { _database = connectionMultiplexer.GetDatabase(); } public IActionResult Index() { _database.StringSet("key", "value"); return View(); }
在GitHub上也有人讨论过这个问题,不过3.0版本 asp.net团队目前没有这么做
– 毕殿卿 5年前https://github.com/aspnet/Extensions/issues/718