将SignalR服务添加为单例,然后向其添加Redis

时间:2019-03-01 08:59:12

标签: asp.net-core redis signalr

你好,我有一个使用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://docs.microsoft.com/en-us/aspnet/core/signalr/redis-backplane?view=aspnetcore-2.2,它希望您将其添加为

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

但是这与我使用SignalR的方式不兼容。无论如何,有什么方法可以使我的实施工作正常进行,或者有人对如何解决这个问题有任何建议?

1 个答案:

答案 0 :(得分:1)

尝试在ConfigureServices中添加以下内容:

private fun onTimerStart(){
        ed.putBoolean("thread", true).apply()
        mRunnable = Runnable {
            seconds = sp.getLong("SECONDS",0)
            try {
                if (seconds!=0L){
                    startRun = true
                    btnStartEnd.text = getString(R.string.stop)
                }
            }catch (ex:Exception){}
            val hours = seconds/3600
            val minutes = (seconds%3600)/60
            val secs = (seconds%60)
            val time = String.format("%d:%02d:%02d", hours, minutes, secs)
            txtClock.text = time
            if (startRun){
                seconds++
                ed.putLong("SECONDS",seconds).apply()
            }
            mHandler.postDelayed(this,1000)
        }
        mHandler.postDelayed(mRunnable,1000)
    }

也将此添加到“配置”中

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

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

别忘了在连接字符串中添加app.UseSignalR(routes => { routes.MapHub<your_Hub>("/yourHub"); });