gpt4 book ai didi

asp.net-core - 将 SignalR 服务添加为单例,然后将 redis 添加到其中

转载 作者:IT王子 更新时间:2023-10-29 06:04:54 26 4
gpt4 key购买 nike

你好,我使用 orleans 和 signalR 启动并运行了一个应用程序,我使用 HubConnectionBuilder 像这样初始化我的 SignalRClient

public async Task<HubConnection> InitSignalRCLient()
{
Program.WriteConsole("Starting SignalR Client...");
var connection = new HubConnectionBuilder()
.ConfigureLogging(logging =>
logging
.AddProvider(new LogProvider(Log.logger, new LogProviderConfiguration
{
Category = LogCategory.SignalR,
Level = LogLevel.Warning
}))
)
.WithUrl(Configuration.GetConnectionString("SignalRInterface"))
.Build();

然后我在配置服务中将服务作为单例添加

services.AddSingleton(SignalRClient)

现在的问题是我想使用 redis 作为背板,我在将 redis 服务添加到我当前使用 SignalR 的方式时遇到了问题这样不行

services.AddSingleton(SignalRClient).AddStackExchangeRedis();

根据文档https://learn.microsoft.com/en-us/aspnet/core/signalr/redis-backplane?view=aspnetcore-2.2它希望你像这样添加它

services.AddSignalR().AddStackExchangeRedis("<your_Redis_connection_string>");

但这不适用于我使用 SignalR 的方式。有没有办法让我的实现工作,或者有人对如何解决这个问题有任何建议?

最佳答案

尝试在 ConfigureServices 中添加:

services.AddDistributedRedisCache(option =>
{
option.Configuration = Configuration.GetConnectionString(<your_Redis_connection_string>);
});

services.AddSignalR().AddStackExchangeRedis(Configuration.GetConnectionString(<your_Redis_connection_string>));

同样在Configure中添加

app.UseSignalR(routes =>
{
routes.MapHub<your_Hub>("/yourHub");
});

并且不要忘记在 connectionStrings 中添加 abortConnect=False

关于asp.net-core - 将 SignalR 服务添加为单例,然后将 redis 添加到其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54941102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com