Skip to content

Add remarks/seealso docs to DI Add*/TryAdd* methods in DependencyInjection.Abstractions#124592

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/update-addsingleton-docs
Draft

Add remarks/seealso docs to DI Add*/TryAdd* methods in DependencyInjection.Abstractions#124592
Copilot wants to merge 4 commits intomainfrom
copilot/update-addsingleton-docs

Conversation

Copy link
Contributor

Copilot AI commented Feb 19, 2026

AddSingleton/AddScoped/AddTransient and their keyed/Try variants lacked documentation explaining multiple-registration semantics and cross-references between Add* and TryAdd* overloads.

Description

  • ServiceCollectionServiceExtensions.cs — Added <remarks> to all AddTransient, AddScoped, AddSingleton overloads clarifying that each call unconditionally appends a registration, GetService returns the last registered instance, and GetServices<T> retrieves all registrations.

  • ServiceCollectionServiceExtensions.Keyed.cs — Same <remarks> added to all AddKeyedTransient, AddKeyedScoped, AddKeyedSingleton overloads, correctly referencing ServiceProviderKeyedServiceExtensions.GetKeyedService{T} and GetKeyedServices{T} (keyed services are not retrievable via the non-keyed GetService/GetServices APIs).

  • ServiceCollectionDescriptorExtensions.cs — Added <seealso> to each TryAddTransient, TryAddScoped, TryAddSingleton overload pointing to the corresponding Add* overload.

  • ServiceCollectionDescriptorExtensions.Keyed.cs — Same <seealso> additions for each TryAddKeyed* overload.

Testing

Documentation-only change; no behavioral modifications.

Original prompt

This section details on the original issue you should resolve

<issue_title>Docs for members in DependencyInjection.Abstractions lack exception information.</issue_title>
<issue_description>### Description

I heard feedback that folks felt the docs for AddSingleton lacked good information about when it would fail and why.

I took a look and I agree. It seems that whenever we have a method with a corresponding Try method, we should be sure to include the exception docs. That will help folks if they need to use the Try method always, or if they would prefer to just use the non-try method for simpler code and treat the exception case as an invalid configuration.

Reproduction Steps

Have a look at docs https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionserviceextensions.addsingleton?view=net-8.0

Expected behavior

Information about meaningful thrown exceptions exists.

Actual behavior

No information exists

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response</issue_description>

<agent_instructions>Improve documentation of methods like AddSingleton and TryAddSingleton.

For AddSingleton, explain in a remark that if AddSingleton is called multiple times for the same service type, all will be added to the collection. But calling GetService for that service type will only retrieve the last registered service. To get all of them, GetServices can be used.

For TryAddSingleton, I think adding a <seealso> to the appropriate overload of AddSingleton is sufficient.

Do this for all relevant methods, not just AddSingleton.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI changed the title [WIP] Update AddSingleton documentation to include exception information Add remarks/seealso docs to DI Add* and TryAdd* methods in DependencyInjection.Abstractions Feb 19, 2026
Copilot AI requested a review from svick February 19, 2026 13:41
Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI changed the title Add remarks/seealso docs to DI Add* and TryAdd* methods in DependencyInjection.Abstractions Improve XML docs for DI Add*/TryAdd* methods in DependencyInjection.Abstractions Feb 19, 2026
Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot AI changed the title Improve XML docs for DI Add*/TryAdd* methods in DependencyInjection.Abstractions Add remarks/seealso docs to DI Add*/TryAdd* methods in DependencyInjection.Abstractions Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docs for members in DependencyInjection.Abstractions lack exception information.

2 participants

Comments