可以只读的 ServiceCollection

可以只读的 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

ec03a502237d0c7c563828c2e7926971.png

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值