-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathServiceProviderExtensions.cs
More file actions
212 lines (166 loc) · 10.6 KB
/
ServiceProviderExtensions.cs
File metadata and controls
212 lines (166 loc) · 10.6 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Configurations;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Messages;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.Requests;
using Microsoft.Testing.Platform.Resources;
using Microsoft.Testing.Platform.Telemetry;
using Microsoft.Testing.Platform.TestHostControllers;
namespace Microsoft.Testing.Platform.Services;
/// <summary>
/// Extension methods for getting services from an <see cref="IServiceProvider" />.
/// </summary>
public static class ServiceProviderExtensions
{
/// <summary>
/// Gets the required service of type <typeparamref name="TService"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="provider">The service provider.</param>
/// <returns>The required service of type <typeparamref name="TService"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="provider"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when the required service is not found.</exception>
public static TService GetRequiredService<TService>(this IServiceProvider provider)
where TService : notnull
{
Ensure.NotNull(provider);
object? service = provider.GetService(typeof(TService));
ApplicationStateGuard.Ensure(service is not null, string.Format(CultureInfo.InvariantCulture, PlatformResources.ServiceProviderCannotFindServiceErrorMessage, typeof(TService)));
return (TService)service;
}
/// <summary>
/// Gets the service of type <typeparamref name="TService"/> from the <see cref="IServiceProvider"/>.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="provider">The service provider.</param>
/// <returns>The service of type <typeparamref name="TService"/> or null if not found.</returns>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="provider"/> is null.</exception>
public static TService? GetService<TService>(this IServiceProvider provider)
where TService : class
{
Ensure.NotNull(provider);
return provider.GetService(typeof(TService)) as TService;
}
/// <summary>
/// Gets the message bus from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The message bus.</returns>
public static IMessageBus GetMessageBus(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredService<IMessageBus>();
/// <summary>
/// Gets the configuration from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The configuration.</returns>
public static IConfiguration GetConfiguration(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredService<IConfiguration>();
/// <summary>
/// Gets the command line options from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The command line options.</returns>
public static ICommandLineOptions GetCommandLineOptions(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredService<ICommandLineOptions>();
/// <summary>
/// Gets the logger factory from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The logger factory.</returns>
public static ILoggerFactory GetLoggerFactory(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredService<ILoggerFactory>();
/// <summary>
/// Gets the output device from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The output device.</returns>
public static IOutputDevice GetOutputDevice(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IOutputDevice>();
/// <summary>
/// Gets the IClientInfo from the <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <returns>The IClientInfo object.</returns>
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")]
public static IClientInfo GetClientInfo(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IClientInfo>();
// Internals extensions
internal static TService GetRequiredServiceInternal<TService>(this IServiceProvider provider)
where TService : notnull
{
Ensure.NotNull(provider);
object? service = ((ServiceProvider)provider).GetServiceInternal(typeof(TService));
ApplicationStateGuard.Ensure(service is not null, string.Format(CultureInfo.InvariantCulture, PlatformResources.ServiceProviderCannotFindServiceErrorMessage, typeof(TService)));
return (TService)service;
}
internal static TService? GetServiceInternal<TService>(this IServiceProvider provider)
where TService : class
{
Ensure.NotNull(provider);
return ((ServiceProvider)provider).GetServiceInternal(typeof(TService)) as TService;
}
internal static IEnumerable<TService> GetServicesInternal<TService>(this IServiceProvider provider)
where TService : notnull
{
Ensure.NotNull(provider);
return ((ServiceProvider)provider).GetServicesInternal(typeof(TService)).Cast<TService>();
}
internal static ITestSessionContext GetTestSessionContext(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestSessionContext>();
internal static IClock GetSystemClock(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IClock>();
internal static ITask GetTask(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITask>();
internal static IPlatformOutputDevice? GetPlatformOutputDevice(this IServiceProvider serviceProvider)
=> serviceProvider.GetServiceInternal<IPlatformOutputDevice>();
internal static IEnvironment GetEnvironment(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IEnvironment>();
internal static ITestApplicationModuleInfo GetTestApplicationModuleInfo(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestApplicationModuleInfo>();
internal static ITestHostControllerInfo GetTestHostControllerInfo(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestHostControllerInfo>();
internal static IProcessHandler GetProcessHandler(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IProcessHandler>();
internal static IClock GetClock(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IClock>();
internal static BaseMessageBus GetBaseMessageBus(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<BaseMessageBus>();
internal static IConsole GetConsole(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IConsole>();
internal static IRuntimeFeature GetRuntimeFeature(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IRuntimeFeature>();
internal static IAsyncMonitorFactory GetAsyncMonitorFactory(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IAsyncMonitorFactory>();
internal static ITestApplicationProcessExitCode GetTestApplicationProcessExitCode(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestApplicationProcessExitCode>();
internal static ITestApplicationCancellationTokenSource GetTestApplicationCancellationTokenSource(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestApplicationCancellationTokenSource>();
internal static ITelemetryInformation GetTelemetryInformation(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITelemetryInformation>();
internal static ITelemetryCollector GetTelemetryCollector(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITelemetryCollector>();
internal static ITestFramework GetTestFramework(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestFramework>();
internal static ITestFrameworkInvoker GetTestFrameworkInvoker(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestFrameworkInvoker>();
internal static IUnhandledExceptionsPolicy GetUnhandledExceptionsPolicy(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IUnhandledExceptionsPolicy>();
internal static ITestExecutionRequestFactory GetTestExecutionRequestFactory(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestExecutionRequestFactory>();
internal static IFileSystem GetFileSystem(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IFileSystem>();
internal static ITestFrameworkCapabilities GetTestFrameworkCapabilities(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<ITestFrameworkCapabilities>();
internal static IPlatformInformation GetPlatformInformation(this IServiceProvider serviceProvider)
=> serviceProvider.GetRequiredServiceInternal<IPlatformInformation>();
internal static IFileLoggerInformation? GetFileLoggerInformation(this IServiceProvider serviceProvider)
=> serviceProvider.GetServiceInternal<IFileLoggerInformation>();
internal static IPlatformOpenTelemetryService? GetPlatformOTelService(this IServiceProvider serviceProvider)
=> serviceProvider.GetServiceInternal<IPlatformOpenTelemetryService>();
}