aspnet / Blazor.Docs

Docs for Blazor
https://blazor.net/
Creative Commons Attribution 4.0 International
131 stars 125 forks source link

Enhance service registration content in Dependency Injection topic #219

Closed danroth27 closed 5 years ago

danroth27 commented 6 years ago

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

MarkStega commented 6 years ago

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

MarkStega commented 6 years ago

[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

guardrex commented 6 years ago

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?

springy76 commented 5 years ago

[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!

guardrex commented 5 years ago

@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.

guardrex commented 5 years ago

Issue has been moved to aspnet/Docs.