可以只读的 ServiceCollection
Intro
在 .NET 7 Preview 4 中,ServiceCollection
可以声明为只读了,这使得我们可以有效避免在构建了 ServiceProvider
之后再新增服务,导致服务注册失败。
Sample
在新的版本中,ServiceCollection
新增了一个 MakeReadonly()
的 API,调用之后,ServiceCollection
就不能再修改了,不能再注册新的服务或者移除集合中的服务,再修改就会抛异常。
直接来看一个示例吧,示例代码如下:
var services = new ServiceCollection();
services.AddSingleton<IUserIdProvider, EnvironmentUserIdProvider>();
await using (var provider = services.BuildServiceProvider())
{Console.WriteLine(provider.GetRequiredService<IUserIdProvider>().GetHashCode());
}Console.WriteLine(services.IsReadOnly);services.MakeReadOnly();Console.WriteLine(services.IsReadOnly);try
{services.AddSingleton<IHttpRequester, HttpClientHttpRequester>();
}
catch (Exception ex)
{Console.WriteLine(ex);
}
输出结果如下:
43942917
False
True
System.InvalidOperationException: The service collection cannot be modified because it is read-only.at Microsoft.Extensions.DependencyInjection.ServiceCollection.ThrowReadOnlyException()at Microsoft.Extensions.DependencyInjection.ServiceCollection.CheckReadOnly()at Microsoft.Extensions.DependencyInjection.ServiceCollection.System.Collections.Generic.ICollection<Microsoft.Extensions.DependencyInjection.ServiceDescriptor>.Add(ServiceDescriptor item)at Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.Add(IServiceCollection collection, Type serviceType, Type implementationType, ServiceLifetime lifetime)at Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton(IServiceCollection services, Type serviceType, Type implementationType)at Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton[TService,TImplementation](IServiceCollection services)at Net7Sample.ServiceCollectionSample.MainTest() in C:\projects\sources\SamplesInPractice\net7Sample\Net7Sample\ServiceCollectionSample.cs:line 28
More
在新的 HostApplicationBuilder
中也借助了这个 API ,在构建 Host
的时候也会调用这个 API 来使得 ServiceCollection
中注册的服务不能再变更,可以参考:https://github.com/dotnet/runtime/pull/68051/files#diff-e55c31d683c37cca99b7a3a274beef4a3101d53b02c9ea989e4f6310094f68ec
在我们的应用中遇到想要使 ServiceCollection
不能再修改的时候就可以考虑使用这个 API 来避免误操作从而导致意外的 BUG
References
https://github.com/dotnet/runtime/pull/68051
https://github.com/dotnet/runtime/pull/68051/files#diff-e55c31d683c37cca99b7a3a274beef4a3101d53b02c9ea989e4f6310094f68ec
https://github.com/dotnet/runtime/issues/66126
https://github.com/WeihanLi/SamplesInPractice/blob/master/net7Sample/Net7Sample/ServiceCollectionSample.cs
.NET 7 中的 HostApplicationBuilder