0

I tried to call ConnectionMultiplexer.ConnectAsync in a Blazor client-side component in the following way:

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();
}

This throws an exception:

children could not be evaluated

How Is it possible to use StackExchange.Redis in a Blazor WebAssembly app?

Edit:

I found a more verbose error message in the VS output:

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

System.NotSupportedException: Cannot start threads on this runtime. at (wrapper managed-to-native) System.Threading.Thread.Thread_internal(System.Threading.Thread,System.MulticastDelegate) at System.Threading.Thread.StartInternal (System.Object principal, System.Threading.StackCrawlMark& stackMark) <0x3b02590 + 0x00008> in :0 at System.Threading.Thread.Start (System.Threading.StackCrawlMark& stackMark) <0x3b02450 + 0x0004e> in :0 at System.Threading.Thread.Start (System.Object parameter) <0x3b022d0 + 0x0003a> in :0 at Pipelines.Sockets.Unofficial.DedicatedThreadPoolPipeScheduler.StartWorker (System.Int32 id) [0x0003a] in C:\Code\Pipelines.Sockets.Unofficial\src\Pipelines.Sockets.Unofficial\DedicatedThreadPoolPipeScheduler.cs:112 at Pipelines.Sockets.Unofficial.DedicatedThreadPoolPipeScheduler..ctor (System.String name, System.Int32 workerCount, System.Int32 useThreadPoolQueueLength, System.Threading.ThreadPriority priority) [0x00072] in C:\Code\Pipelines.Sockets.Unofficial\src\Pipelines.Sockets.Unofficial\DedicatedThreadPoolPipeScheduler.cs:74 at StackExchange.Redis.SocketManager..ctor (System.String name, System.Int32 workerCount, StackExchange.Redis.SocketManager+SocketManagerOptions options) [0x0006e] in //src/StackExchange.Redis/SocketManager.cs:98 at StackExchange.Redis.SocketManager..ctor (System.String name, System.Int32 workerCount, System.Boolean useHighPrioritySocketThreads) [0x00000] in //src/StackExchange.Redis/SocketManager.cs:44 at StackExchange.Redis.SocketManager.get_Shared () [0x0000c] in //src/StackExchange.Redis/SocketManager.cs:132 at StackExchange.Redis.ConnectionMultiplexer.OnCreateReaderWriter (StackExchange.Redis.ConfigurationOptions configuration) [0x00000] in //src/StackExchange.Redis/ConnectionMultiplexer.ReaderWriter.cs:9 at StackExchange.Redis.ConnectionMultiplexer..ctor (StackExchange.Redis.ConfigurationOptions configuration) [0x000d6] in //src/StackExchange.Redis/ConnectionMultiplexer.cs:1150 at StackExchange.Redis.ConnectionMultiplexer.CreateMultiplexer (System.Object configuration, StackExchange.Redis.ConnectionMultiplexer+LogProxy log, System.EventHandler`1[StackExchange.Redis.ConnectionFailedEventArgs]& connectHandler) [0x0000d] in //src/StackExchange.Redis/ConnectionMultiplexer.cs:957 at StackExchange.Redis.ConnectionMultiplexer.ConnectImplAsync (System.Object configuration, System.IO.TextWriter log) [0x0003a] in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:854 at Joker.BlazorApp.Sample.Pages.ProductsComponentBase.OnInitializedAsync () [0x0007c] in C:\Users\tomas.fabian\source\repos\Joker.BlazorApp.Sample\Joker.BlazorApp.Sample\Pages\ProductsComponentBase.cs:52 at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x37da140 + 0x0013a> in :0

Vencovsky
  • 17,142
  • 1
  • 46
  • 97
Kubus
  • 46
  • 1
  • 5

1 Answers1

0

You cannot use StackExchange Redis in blazor webassembly because that is like an SDK for connecting and managing the redis, not the redis it self.

And the error message saying

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

Means that it cannot run the redis in webassembly.

If you want to use StackExchange Redis, you need to create some api that will be connected with the redis.

If you want to cache some data in the client side, you can either user localStorage/sessionStorage or IndexedDB.

Vencovsky
  • 17,142
  • 1
  • 46
  • 97
  • I wanted to use StackExchange.Redis for pub/sub, mainly the `ISubscriber` part. I found out that `ConnectionMultiplexer` uses a `SocketManager`. Maybe a different implementation could work, in my opinion this is a threading issue. For example this works in WebAssembly `TaskFactory().StartNew(() => {})`, but this doesn't `new Thread(() =>{}_.Start()`. – Kubus Jun 15 '20 at 12:42
  • `new Thread` throws the same exception: Unhandled exception rendering component: Cannot start threads on this runtime. System.NotSupportedException: Cannot start threads on this runtime. – Kubus Jun 15 '20 at 12:44
  • @TomasFabian take a look at this issue on github: [Does Blazor WebAssembly support multi threading?](https://github.com/dotnet/aspnetcore/issues/14253) and you will see that you cannot instantiate a new `Thread`. So you are right, this is a threading issue – Vencovsky Jun 15 '20 at 12:49
  • tnx, thats what I found out, too. It seems that the TPL part is working to some extent, but the direct use of thread pool, can't schedule the work. I tried this, but it didn't work in netstandard 2.1: https://platform.uno/blog/webassembly-threading-in-net/. I'm curious now, whether SignalR client works in web assembly. It uses web sockets AFAIK. – Kubus Jun 15 '20 at 13:01