Blazor Startup - Pre-registered Services

S Chapman 82 41 Reputation points
2021-01-21T10:07:12.25+00:00

Hi I'm new to Blazor ad C# 6.0 and trying to find my feet. I am learning about dependency injection currently. Whilst I appreciate why DI is needed I am struggling a bit with documentation. From the documentation it is clear that you first need to register your service with the DI container and then use either the @inject or the [Inject] attribute to get access to the service. But what has stumped me is that for IJSRuntime you don't need to register, you can simply inject.

Is there a definitive list of all such services that don't need explicit registering and are readily available in the framework to inject?

Please can someone shed some light on this. It will be great help.

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,479 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,579 questions
0 comments No comments
{count} votes

Accepted answer
  1. Duane Arnold 3,216 Reputation points
    2021-01-21T18:44:24.813+00:00

    A runtime implements portions of an execution model, which would be for JavaScript in this case. A runtime is not an object that needs to be instanced and injected into a an object.

    You can inject IJSRuntime or any Interface into an object as long as the code the Interface represents is running or that the object the Interface represents has been instanced in order to use their behaviors/methods of objects in the runtime or on an instanced object.

    https://en.wikipedia.org/wiki/Runtime_system

    Is there a definitive list of all such services that don't need explicit registering and are readily available in the framework to inject?

    No, none that I know about.

    But I feel that you need to understand the purpose of DI.

    https://ardalis.com/new-is-glue/

    https://video2.skills-academy.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection

    You need to understand the purpose of an Interface.

    https://www.c-sharpcorner.com/blogs/understanding-interfaces-via-loose-coupling-and-tight-coupling


1 additional answer

Sort by: Most helpful
  1. Ahmed Alejo 76 Reputation points
    2021-01-23T08:55:57.817+00:00

    The well-known services are added either via the Startup file.

    csharp
    public void ConfigureServices(IServiceCollection services)
    {
           //Services like IJSRuntime are added here.
           service.AddServierSideBlazor();
    }
    
    //or with Blazor Assembly
    WebAssemblyHostBuilder.CreateDefault(args)
    

    The above is for blazor, you could have .Add[Something] (e.g AddMvc(), AddSignalR(), AddHttpClient)
    Which basically mean

    Add services needed for the [Mvc|SignalR|HttpClient] to work.

    The list of services for a given functionality is normally mentioned in its respective docs

    0 comments No comments