Skip to content

[API Proposal]: Add ServiceCollection.MakeReadOnly() #66126

Closed

Description

@halter73

Background and motivation

The new HostApplicationBuilder in Microsoft.Extensions.Hosting and the MAUI host both implement custom IServiceCollection that change to read-only after the host is built. This means that ICollection<ServiceDescriptor>.IsReadOnly becomes true and any attempts to mutate the collection result in an InvalidOperationException. This makes it clear to developers that any attempt to modify services at this point is futile.

It would be nice if these hosts could use the default ServiceCollection to take advantage of potential perf improvements like those described in #44728.

API Proposal

namespace Microsoft.Extensions.DependencyInjection
{
    public class ServiceCollection
    {
+      public void MakeReadOnly();
    }
}

API Usage

public ServiceCollection ServiceCollection { get; } = new();

IHost Build()
{
    // BuildServiceProvider() is an extension method on IServiceProvider so it cannot
    // make the ServiceCollection read-only itself. It could after this change if it attempted to cast,
    // but that would be a breaking change and is not what I'm proposing.
    IServiceProvidier serviceProvider = ServiceCollection.BuildServiceProvider();
    ServiceCollection.MakeReadOnly();
    return serviceProvider.GetRequiredService<IHost>();
}

Alternative Designs

namespace Microsoft.Extensions.DependencyInjection
{
    public class ServiceCollection
    {
-      public bool IsReadOnly { get; }
+      public bool IsReadOnly { get; set; }
    }
}

I considered adding a setter to IsReadOnly, but I don't think it should be possible to transition a ServiceCollection from a read-only to a mutable state.

Risks

It adds additional API to ServiceCollection that shouldn't be called by the majority of developers interacting with the type. It's only really useful to library authors who build the ServiceCollection.

Context: #65109 (comment)

@eerhardt

Activity

  1. added
    api-suggestion
    on Mar 3, 2022
  2. ghost added
    untriaged
    on Mar 3, 2022
  3. added this to the 7.0.0 milestone on Mar 3, 2022
  4. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Mar 3, 2022
  5. terrajobst commented on Apr 14, 2022

    @terrajobst
    Contributor
    • Looks good as proposed
    namespace Microsoft.Extensions.DependencyInjection;
    
    public partial class ServiceCollection
    {
        public void MakeReadOnly();
    }
  6. added
    api-approved
    and removed
    api-ready-for-review
    on Apr 14, 2022
  7. added
    help wanted
    on Apr 14, 2022
  8. self-assigned this
    on Apr 14, 2022
  9. removed
    help wanted
    on Apr 14, 2022
  10. added a commit that references this issue on Apr 14, 2022
    a01d3c8
  11. ghost added
    in-pr
    on Apr 14, 2022
  12. added a commit that references this issue on Apr 15, 2022
    8d9daab
  13. ghost removed
    in-pr
    on Apr 15, 2022
  14. ghost locked as resolved and limited conversation to collaborators on May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-Extensions-DependencyInjection

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions