-1
    public class Program {
        public static async Task Main( string[] args ) {
            var builder = WebAssemblyHostBuilder.CreateDefault( args );
            builder.Services.AddSingleton<ClientDataAccess>();
            await builder.Build().RunAsync();
        }
    }

Why isn't this code working to add ClientDataAccess as a DI service in my clientside (WASM) Blazor? It doesn't matter if the service is an empty class, or whether I inject it as a singleton or not. It simply produces this exception:

InvalidOperationException: Cannot provide a value for property 'Data' on type 'MyProgram.Client.Components.MyEditor'. There is no registered service of type 'MyProgram.Client.Services.ClientDataAccess'.

This is for a .net 8 blazor web application that uses client (WASM) Blazor with server-side hosting and REST API.

4
  • You've shown how you've added the service to the container builder, but not how you're trying to get the service from the container. The title asks a different question than the body. The one in the title is quite opinionated (and therefore off topic - consider just asking about the error). Commented Feb 29 at 16:01
  • I can either use [Inject] public ClientDataAccess Data { get; set; } = null!; in a component's partial class or @inject ClientDataAccess Data in the Blazor code. It makes no difference.
    – hamstar
    Commented Feb 29 at 16:04
  • 1
    Edit your question to add clarification. Commented Feb 29 at 16:05
  • I might be overlooking something, but I think there's a different context to blazor running on the client (WASM), and therefore the concept of DI services doesn't have the same relevance. If I'm wrong, feel free to ignore that matter and answer the literal question, though. This should not need explaining.
    – hamstar
    Commented Feb 29 at 17:15

1 Answer 1

0

The posted code should work for a standalone Wasm App.

When using the "Blazor Web App", with either rendermode WebAssembly or Auto, you will have to register the service twice: in both Program.cs files. You only have to implement it once, in the Client.

You will probably have to rethink what a Singleton ClientDataAccess can do in the client. Note that you will have 2 'singleton per platform' instances.

5
  • The only use for the singleton on the client is to make server calls, which doesn't really need a service but I'm using one anyway for consistency. There might not be a true need for DI on the client, but that's it's own topic. I was hoping that point would be conveyed by the title, but apparently that just triggered kneejerk downvotes...
    – hamstar
    Commented Feb 29 at 18:47
  • I use Client side services a lot, like Singletons for State management. Everything else is usually Transient. Commented Feb 29 at 19:25
  • Would those singletons really need to exist in service form on the client? I think there's a specific purpose for services on the server, due to cloud architecture or such stuff that I don't yet understand properly.
    – hamstar
    Commented Feb 29 at 19:40
  • DI is just a way to organize things. A good way, but you're right that in the Client they are a little less useful. But the framework already uses it everywhere, just go with the flow. Commented Feb 29 at 20:51
  • That's precisely what I'm trying to do, but not if it's gonna fight me. I think I can get your method to work, though.
    – hamstar
    Commented Feb 29 at 21:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.