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 ...
@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.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@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 ...
The different patterns for service registration should be covered in the DI docs. I agree we can close this one.