When debugging Blazor Server, Blazor WASM's invoke is jumping to Server side

Matěj Rada 40 Reputation points
2024-02-23T20:18:14.7966667+00:00

I want to share the same data class between Blazor Server and Blazor WASM with the injection of DataSource in such a way that once registered for Server, it will use EntityFramework by DbContext, and once registered for WASM, it will use DbSet for remote Linq. But since I am debugging it from the same solution, WASM is not registering any services and instead my code is jumping into Blazor Server's registered Singleton. Once I remove registration from Server, Razor page with @inject will say that there is no service for such an injection. I have an unmodified sample Blazor server project generated by VisualStudio. I just added .NET Standard library with shared functionality and now I am trying to configure Services. On the server side everything working.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,665 questions
{count} votes

Accepted answer
  1. Rada Matěj 200 Reputation points
    2024-02-24T19:48:49.9333333+00:00

    So, the problem is server-side rendering, which is also server-side resolving injections. After hours of researching, this is how to ensure only local WASM's injection will be used:

    @inject IServiceProvider ServiceProvider
    
    var factory = ServiceProvider.GetRequiredService<IDataSourceFactory>();
    

    So, I will never use @inject ever, because this is really stupid idea to not provide clear vision where will be injection resolved. Aaaaaaaa.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 73,081 Reputation points
    2024-02-24T00:13:12.71+00:00

    Your use case is not clear. Why would you use a DBSet in Blazor WASM? It does not have access to a dbcontext (though there are samples of using SqlLite). You should be using the your EF POCO entries in the WASM.

    Blazor WASM does not have network access, it needs to call javascript to access the network. Blazor's WASM WebClient() does this. If you want to use Remote Linq, you may need to write your own wrapper as wcf is not supported in Blazor WASM and is used by their default sample code. You will want a pure HttpClient() based api.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.