Skip to content

Improve debugging experience of Microsoft.Extensions.DependencyInjection #86553

Closed

Description

@JamesNK

Microsoft.Extensions.DependencyInjection debug experience could be improved.

I think there are two primary APIs to focus on: ServiceCollection and IServiceProvider implementations.

ServiceCollection is already pretty good. It could be improved with a debugger display that shows the count and whether it's read-only.

image

IServiceProvider is difficult to debug. Useful information to make available:

  • Its service descriptors (the information that ServiceCollection displays)
  • Is it a scoped or root service provider
  • Is it disposed
  • Realized services
  • Registered disposable services
  • More that I haven't thought of?

image

Activity

  1. ghost added
    untriaged
    on May 21, 2023
  2. added
    enhancement
    and removed
    untriaged
    on May 23, 2023
  3. added this to the 8.0.0 milestone on May 23, 2023
  4. ericstj commented on May 23, 2023

    @ericstj
    Member

    @steveharter this seems like a nice thing to improve in 8.0.

  5. kwaazaar commented on Jun 13, 2023

    @kwaazaar

    I would like something that helps with detecting an attempt to resolve a circular dependency that was not detected on build (eg when using a factory, like services.AddTransient<IService>(sp => sp.GetRequiredService<MyService>())).

    I know my codebase needs refactoring, but it's so big (and not created by myself), that I have no idea where those circular dependencies are. Result is now that I have to start the application and see if memory usage will suddenly increase dramatically (result of the circular dependency) and then manually check all code to find where the cause could be.

    Logging could help, but detecting on build OR resolve would help even more.
    And since I know from my factory-code which types I create, I could even provide it to the TryAdd/Add on the servicecollection, so it could use that information for detection. Something like this: services.AddTransient<IService>(sp => sp.GetRequiredService<MyService>(), new[] { typeof(MyService) }).

  6. JamesNK commented on Jul 4, 2023

    @JamesNK
    MemberAuthor

    Fixed by #88082

  7. ghost locked as resolved and limited conversation to collaborators on Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-DependencyInjectionenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions