首页 > 解决方案 > 如何在 Blazor WebAssembly 应用程序中使用 StackExchange.Redis?

问题描述

我尝试ConnectionMultiplexer.ConnectAsync通过以下方式调用 Blazor 客户端组件:

protected override async Task OnInitializedAsync()
{
  var configuration = new ConfigurationOptions
  {
    AbortOnConnectFail = false,
    ConnectTimeout = 3000,
    SyncTimeout = 5000,
    KeepAlive = 180,
    EndPoints =
    {
      {
        "localhost", 6379
      }
    }
  };

  await ConnectionMultiplexer.ConnectAsync(configuration);

  await base.OnInitializedAsync();
}

这会引发异常:

无法评估孩子

如何StackExchange.RedisBlazor WebAssembly应用程序中使用?

编辑:

我在 VS 输出中发现了更详细的错误消息:

  Unhandled exception rendering component: Cannot start threads on this runtime.

System.NotSupportedException:无法在此运行时启动线程。在 System.Threading.Thread.StartInternal(System.Object 主体,System.Threading.StackCrawlMark 和 stackMark)的(包装器托管到本机)System.Threading.Thread.Thread_internal(System.Threading.Thread,System.MulticastDelegate)<0x3b02590 + 0x00008> in :0 at System.Threading.Thread.Start (System.Threading.StackCrawlMark& stackMark) <0x3b02450 + 0x0004e> in :0 at System.Threading.Thread.Start (System.Object 参数) <0x3b022d0 + 0x0003a> in :0 在 Pipelines.Sockets.Unofficial.DedicatedThreadPoolPipeScheduler.StartWorker (System.Int32 id) [0x0003a] 在 C:\Code\Pipelines.Sockets.Unofficial\src\Pipelines.Sockets.Unofficial\DedicatedThreadPoolPipeScheduler.cs:112 在 Pipelines.Sockets .Unofficial.DedicatedThreadPoolPipeScheduler../src/StackExchange.Redis/SocketManager.cs:98 在 StackExchange.Redis.SocketManager..ctor (System.String name, System.Int32 workerCount, System.Boolean useHighPrioritySocketThreads) [0x00000] in //src/StackExchange.Redis/SocketManager .cs:44 在 StackExchange.Redis.SocketManager.get_Shared () [0x0000c] in / /src/StackExchange.Redis/SocketManager.cs:132 在 StackExchange.Redis.ConnectionMultiplexer.OnCreateReaderWriter (StackExchange.Redis.ConfigurationOptions 配置) [0x00000]在 / /src/StackExchange.Redis/ConnectionMultiplexer.ReaderWriter.cs:9 在 StackExchange.Redis.ConnectionMultiplexer..ctor (StackExchange.Redis.ConfigurationOptions 配置) [0x000d6] 在 //src/StackExchange.Redis/ConnectionMultiplexer.cs:1150 在 StackExchange.Redis.ConnectionMultiplexer.CreateMultiplexer(System.Object 配置,StackExchange.Redis.ConnectionMultiplexer+LogProxy 日志,System.EventHandler`1[StackExchange.Redis.ConnectionFailedEventArgs]&connectHandler) [0x0000d] 在 //src/StackExchange.Redis/ConnectionMultiplexer.cs:957 在 /_/src/StackExchange.Redis/ConnectionMultiplexer.cs 中的 StackExchange.Redis.ConnectionMultiplexer.ConnectImplAsync(System.Object 配置,System.IO.TextWriter 日志)[0x0003a]: 854 在 Joker.BlazorApp.Sample.Pages.ProductsComponentBase.OnInitializedAsync () [0x0007c] 在 C:\Users\tomas.fabian\source\repos\Joker.BlazorApp.Sample\Joker.BlazorApp.Sample\Pages\ProductsComponentBase.cs: 52 在 Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x37da140 + 0x0013a> 在:0

标签: blazorstackexchange.redisblazor-client-sideblazor-webassembly

解决方案


您不能在 blazor webassembly 中使用 StackExchange Redis,因为这就像一个用于连接和管理 redis 的 SDK,而不是它本身的 redis。

错误信息说

未处理的异常呈现组件:无法在此运行时启动线程。

意味着它不能在 webassembly 中运行 redis。

如果要使用 StackExchange Redis,则需要创建一些将与 redis 连接的 api。

如果你想在客户端缓存一些数据,你可以使用 user localStorage/sessionStorageIndexedDB.


推荐阅读