/
ServiceProviderKeyedServiceExtensions.cs
101 lines (88 loc) · 5.45 KB
/
ServiceProviderKeyedServiceExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for getting services from an <see cref="IServiceProvider" />.
/// </summary>
public static class ServiceProviderKeyedServiceExtensions
{
/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="provider">The <see cref="IServiceProvider"/> to retrieve the service object from.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns>A service object of type <typeparamref name="T"/> or null if there is no such service.</returns>
public static T? GetKeyedService<T>(this IServiceProvider provider, object? serviceKey)
{
ThrowHelper.ThrowIfNull(provider);
if (provider is IKeyedServiceProvider keyedServiceProvider)
{
return (T?)keyedServiceProvider.GetKeyedService(typeof(T), serviceKey);
}
throw new InvalidOperationException(SR.KeyedServicesNotSupported);
}
/// <summary>
/// Get service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="provider">The <see cref="IServiceProvider"/> to retrieve the service object from.</param>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns>A service object of type <paramref name="serviceType"/>.</returns>
/// <exception cref="System.InvalidOperationException">There is no service of type <paramref name="serviceType"/>.</exception>
public static object GetRequiredKeyedService(this IServiceProvider provider, Type serviceType, object? serviceKey)
{
ThrowHelper.ThrowIfNull(provider);
ThrowHelper.ThrowIfNull(serviceType);
if (provider is IKeyedServiceProvider requiredServiceSupportingProvider)
{
return requiredServiceSupportingProvider.GetRequiredKeyedService(serviceType, serviceKey);
}
throw new InvalidOperationException(SR.KeyedServicesNotSupported);
}
/// <summary>
/// Get service of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="provider">The <see cref="IServiceProvider"/> to retrieve the service object from.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns>A service object of type <typeparamref name="T"/>.</returns>
/// <exception cref="System.InvalidOperationException">There is no service of type <typeparamref name="T"/>.</exception>
public static T GetRequiredKeyedService<T>(this IServiceProvider provider, object? serviceKey) where T : notnull
{
ThrowHelper.ThrowIfNull(provider);
return (T)provider.GetRequiredKeyedService(typeof(T), serviceKey);
}
/// <summary>
/// Get an enumeration of services of type <typeparamref name="T"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="T">The type of service object to get.</typeparam>
/// <param name="provider">The <see cref="IServiceProvider"/> to retrieve the services from.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns>An enumeration of services of type <typeparamref name="T"/>.</returns>
public static IEnumerable<T> GetKeyedServices<T>(this IServiceProvider provider, object? serviceKey)
{
ThrowHelper.ThrowIfNull(provider);
return provider.GetRequiredKeyedService<IEnumerable<T>>(serviceKey);
}
/// <summary>
/// Get an enumeration of services of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="provider">The <see cref="IServiceProvider"/> to retrieve the services from.</param>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <param name="serviceKey">An object that specifies the key of service object to get.</param>
/// <returns>An enumeration of services of type <paramref name="serviceType"/>.</returns>
[RequiresDynamicCode("The native code for an IEnumerable<serviceType> might not be available at runtime.")]
public static IEnumerable<object?> GetKeyedServices(this IServiceProvider provider, Type serviceType, object? serviceKey)
{
ThrowHelper.ThrowIfNull(provider);
ThrowHelper.ThrowIfNull(serviceType);
Type? genericEnumerable = typeof(IEnumerable<>).MakeGenericType(serviceType);
return (IEnumerable<object>)provider.GetRequiredKeyedService(genericEnumerable, serviceKey);
}
}
}