supabase-community / supabase-csharp

A C# Client library for Supabase
https://github.com/supabase-community/supabase-csharp/wiki
MIT License
502 stars 51 forks source link

Blazor WASM - Initialising client with realtime produces error 'System.Security.Cryptography.Csp is not supported on this platform.' #21

Closed d11-jwaring closed 2 years ago

d11-jwaring commented 2 years ago

Good evening,

I'm using Visual Studio 2022 to build a .NET 6 Blazor Web Assembly app. I've used the supabase-csharp nuget package to connect to login and fetch from the database. However, it doesn't appear the real-time connections work due to the following error: System.Security.Cryptography.Csp is not supported on this platform.

Here's the error full log i get: image

It looks like this is a result of an update from Microsoft: https://docs.microsoft.com/en-us/dotnet/core/compatibility/cryptography/5.0/cryptography-apis-not-supported-on-blazor-webassembly

So I'm not sure if you are going to be able to address this. I'll do some digging but I doubt i'll get anywhere.

Here is the code I'm using to initialise the supabase client which produces the error:

await Supabase.Client.InitializeAsync(
     supabaseUrl,
     supabaseKey,
     new Supabase.SupabaseOptions
     {
         AutoConnectRealtime = true, // Only running with this open executes without error but realtime object is null
         ShouldInitializeRealtime = true, // Produces System.Security.Cryptography.Csp error
     }
 );

All the best, John Waring.

acupofjose commented 2 years ago

Thanks for raising the issue John! Unfortunately supporting blazor websockets would require rewriting the realtime-csharp library using a different Websocket Client dependency (This is discussed a bit on #3). I haven't seen enough interest from people to justify rewriting that library, so the current solution is, well, to disable realtime on blazor apps.

If you have an interest in trying a different Websocket library, I'm all ears! But I can't guarantee it will be implemented.

d11-jwaring commented 2 years ago

Thanks for the fast reply. No problem it does look like a lot of work. I'll have a look at what may work in the Blazor Web Assembly environment, or see if i can find a resolution for the cryptography dll issue. But I doubt I'll get anywhere.

On a side note, the Blazor Server seems fine initialising the above code, I'll test it further tomorrow to see how functional it is, but not sure the supabase client in blazor server will work for me.

Kind Regards, John Waring.

d11-jwaring commented 2 years ago

I'm still investigating other libraries I could possibly try to replace the 'WebSocketSharp-netstandard' package. When I finish work I'm going to have an attempt to change the Websocket code to use: https://github.com/Marfusios/websocket-client

I doubt I'll be successful, but it's worth a try.

acupofjose commented 2 years ago

@d11-jwaring awesome! Keep me in the loop if you hit any brick walls!

d11-jwaring commented 2 years ago

Ok, so far so good. I've cloned the realtime-csharp repository and set up the Websocket.Client package and removed 'WebSocketSharp-netstandard' package. This should avoid the above error I was getting in Blazor WASM, but I still need to test that.

The unit tests pass and the 'RealtimeExample' csproj runs without issues as well.

I'll work on testing this in a Blazor WASM client now, fingers crossed this will do the job.

d11-jwaring commented 2 years ago

Ok, all seems fine, which I'm quite delightfully surprised: image

I built a local nuget package of realtime-csharp with me changes, added it to my local nuget source and spun up a Blazor WASM project and added postgrest nuget package and the local realtime-csharp package. And it connected to the docker containers without issue.

Here's the Blazor WASM project and realtime-csharp nuget package to demonstrate the fix: https://github.com/d11-jwaring/SupabaseRealtimeBlazorWASM/tree/master

Let me know how I can send the changes over so you can review and ensure it's a good solution and won't impact your other users.

acupofjose commented 2 years ago

Wow! Did that quick @d11-jwaring! Sounds like you were selling yourself short there - haha! If you could fork the repo, commit the changes you've made, and then open a pull request so I can review (and hopefully merge) the changes, that would be fantastic!

d11-jwaring commented 2 years ago

I've setup a PR on the realtime-csharp repo: https://github.com/supabase-community/realtime-csharp/pull/14

acupofjose commented 2 years ago

@d11-jwaring will be available in supabase-csharp@0.3.2 and realtime-csharp@3.0.0 - thanks for the help!