-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: Add ServiceCollection.MakeReadOnly() #66126
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsBackground and motivationThe new It would be nice if these hosts could use the default API Proposalnamespace Microsoft.Extensions.DependencyInjection
{
public class ServiceCollection
{
+ public void MakeReadOnly();
}
} API Usagepublic 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 Designsnamespace Microsoft.Extensions.DependencyInjection
{
public class ServiceCollection
{
- public bool IsReadOnly { get; }
+ public bool IsReadOnly { get; set; }
}
} I considered adding a setter to RisksIt adds additional API to
|
namespace Microsoft.Extensions.DependencyInjection;
public partial class ServiceCollection
{
public void MakeReadOnly();
} |
With new hosting API patterns, it is useful to be able to mark a ServiceCollection as read-only and disallow for any more modifications to it. Fix dotnet#66126
With new hosting API patterns, it is useful to be able to mark a ServiceCollection as read-only and disallow any more modifications to it. Fix #66126
Background and motivation
The new
HostApplicationBuilder
in Microsoft.Extensions.Hosting and the MAUI host both implement customIServiceCollection
that change to read-only after the host is built. This means thatICollection<ServiceDescriptor>.IsReadOnly
becomes true and any attempts to mutate the collection result in anInvalidOperationException
. 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
Alternative Designs
I considered adding a setter to
IsReadOnly
, but I don't think it should be possible to transition aServiceCollection
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 theServiceCollection
.Context: #65109 (comment)
@eerhardt
The text was updated successfully, but these errors were encountered: