Aspnetcore.docs: Enhance service registration content

Created on 2 Feb 2019  Â·  6Comments  Â·  Source: dotnet/AspNetCore.Docs

From danroth27 at https://github.com/aspnet/Blazor.Docs/issues/219 ...

_From @MarkStega on August 11, 2018 14:32_

If I follow the pattern shown in the Blazor DI documentation and have two services, RestBaseService & RestProcessService that have an injection of

services.AddTransient<IRestBaseService, RestBaseService>();
services.AddTransient<IRestProcessService, RestProcessService>();

then I can't have a constructor injection that refers to RestBaseService; it has to refer to IRestBaseService like this

public RestProcessService(
        IRestBaseService p_RestBaseService,
        ILogger<LoggingFramework> p_Logger)
{
    m_RestBaseService = (RestBaseService)p_RestBaseService;
    m_Logger = p_Logger;
    p_Logger.LogDebug("RestProcessService constructor");
}

If I try that same constructor with RestBaseService an error is thrown saying that that type can not be resolved.

If I change the service registration to just reference the class

services.AddTransient<RestBaseService>();
services.AddTransient<RestProcessService>();

then I can use the class name in constructors. Then in BrowserServiceProvider.cs I see yet another form of service registration that appears to add explicit instances to the services collection

private void AddDefaultServices(ServiceCollection serviceCollection)
{
    serviceCollection.AddSingleton<IUriHelper>(BrowserUriHelper.Instance);
    serviceCollection.AddSingleton(new HttpClient(new BrowserHttpMessageHandler())
        {
            BaseAddress = new Uri(BrowserUriHelper.Instance.GetBaseUri())
        });
}

All said, I think that the DI documentation page needs a little beefing up to explain the different scenarios because if you follow its guidance now the outcome is poor.

_Copied from original issue: aspnet/Blazor#1297_

From @MarkStega ...

[Addition] If someone can give me some guidance I'll volunteer to update the page...

From @MarkStega ...

[Addition] Blazor doesn't currently have the concept of DI scopes. Scoped behaves like Singleton. Therefore, prefer Singleton and avoid Scoped. is no longer correct with SSR

From @guardrex ...

Hello @MarkStega ... To submit updates to the topic, do this ...

  1. Navigate to the page: https://blazor.net/docs/dependency-injection.html
  2. Click the Improve this doc link above the right sidebar.
  3. Click the pencil icon (over where the Raw, Blame, and History buttons appear).
  4. Edit the file
  5. Create the branch and submit. You can give it a name or leave the default "Update dependency-injection.md" name. You don't need to add a commit message. You'll be able to provide details about the change in the PR's opening comment.

@rstropek Do you have any remarks about what @MarkStega has posted above :point_up: before he gets started with updates?

From @springy76 ...

> [Addition] Blazor doesn't currently have the concept of DI scopes. Scoped behaves like Singleton. Therefore, prefer Singleton and avoid Scoped. is no longer correct with SSR

This is not even not correct, it's diametral wrong!

From @guardrex ...

@springy76 It's in progress :point_right: https://github.com/aspnet/Blazor.Docs/pull/308

The release of 2.2 slowed things down a bit. We should be picking up some steam with Blazor/Razor Components shortly.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Blazor P3

All 6 comments

@danroth27 I reviewed what we did on https://github.com/aspnet/Blazor.Docs/pull/308 ... I think we're ok here (i.e, we can close this). We took @bradygaster's advice ... and if you can't take Brady's advice, you may as well cut a chicken open to find out what to do. :smile:

WHAT?

@guardrex i had just popped an altoid when i read this. i laughed and said altoid went the WRONG way (down). you almost killed me with this one!

@bradygaster :smile: We took @vertonghenb advice that you were ok with at https://github.com/aspnet/Blazor.Docs/pull/308#discussion_r241964377.

I'm work'in down to P3 Blazor issues. Trying to figure out if we can just close this one. It certainly isn't one that readers are pinging us on.

@danroth27 Looking this over. TL;DR ...

  • Service registration scenarios are better for the main DI (services) topic, not here imo. Here, I think it best for us to concentrate on one quick example (that we have) and Blazor-specific scenarios (that we also have). I've opened https://github.com/aspnet/AspNetCore.Docs/issues/12734 to work on the DI service extension coverage in the ASP.NET Core DI (Services) topic. [EDIT: I originally marked that for the PU asking for notes. I'd like to research that on my own out of personal interest, so I've removed the PU label for the time being. I might have time in July.]
  • "Blazor doesn't currently have the concept of DI scopes. Scoped behaves like Singleton. Therefore, prefer Singleton and avoid Scoped. is no longer correct with SSR" ... "This is not even not correct, it's diametral wrong!" ... How can this be addressed, if required?

The different patterns for service registration should be covered in the DI docs. I agree we can close this one.

Was this page helpful?
0 / 5 - 0 ratings