Skip to content

[Dependency injection] Include a topic about how the service collection works #21235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
javiercn opened this issue Oct 10, 2019 · 3 comments
Closed
Labels
won't fix Issues that were closed as part of automated backlog grooming

Comments

@javiercn
Copy link
Member

javiercn commented Oct 10, 2019

  • We get many issues related to how to override different things for integration tests.
  • All of these questions are never related to our integration testing support but about how DI works and how to effectively replace services for testing.
  • This requires a proper understanding of the semantics of the service collection and how the service provider is built.
  • There are no docs for it on the official documentation.

The ask here is to include a couple of sections on DI (and potentially hosting too) explaining how all this works. Specifically:

  • In the hosting docs, explain the order in which ConfigureServices callbacks get scheduled.
    • Include an example with an app calling host.ConfigureServices before calling webHost.UseStartup and then host.ConfigureServices after it, to demonstrate the order in which the callbacks get run. Something like (note it's hand-coded on this comment)
    var host = hostBuilder
        .ConfigureServices(sc => sc.AddTransient<ITestService,BeforeService>())
        .ConfigureWebHostDefaults(whb => whb.UseStartup<Startup>())
        .ConfigureServices(sc => sc.AddTransient<ITestService,AfterService>())
        .Build();
    
    Console.WriteLine(host.Services.GetRequiredService<ITestService>().GetType())
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services) => services.AddTransient<ITestService,Service>();
      
        public void Configure(IApplicationBuilder app) => { };
     }
    
     public interface ITestService { }
     public class BeforeService : ITestService { }
     public class Service : ITestService { }
     public class AfterService : ITestService { }
  • In the dependency injection docs use an example similar to the one above to showcase:
    • How to replace services from the service collection.
      • Show that adding a service later in the collection "overrides" an existing service definition.
    • The difference between TryAdd and Add (I know there's a section for it, but it needs a sample)
      • Showcase calling TryAdd with and without an existing service registered.

Document Details

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

@Rick-Anderson
Copy link
Contributor

@scottaddie can you make this a priority?

Copy link

According to the Microsoft's document seems that AddTransient is better than AddScope but I still can't understand why some developers use AddScope.

@Rick-Anderson Rick-Anderson transferred this issue from dotnet/AspNetCore.Docs Oct 26, 2020
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Oct 26, 2020
@PRMerger6 PRMerger6 added the Pri2 label Dec 8, 2020
@dotnet-bot dotnet-bot added the won't fix Issues that were closed as part of automated backlog grooming label Jan 25, 2021
@dotnet-bot
Copy link
Contributor

This issue has been closed as part of the issue backlog grooming process outlined in #22351.

That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
won't fix Issues that were closed as part of automated backlog grooming
Projects
None yet
Development

No branches or pull requests

7 participants