-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
35 lines (27 loc) · 1.26 KB
/
Helpers.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
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace TheElm.Services {
public static partial class Helpers {
public static T GetRequiredOptions<T>( this IServiceProvider provider ) where T : class {
_ = provider ?? throw new ArgumentNullException(nameof(provider));
return provider.GetRequiredService<IOptions<T>>()
.Value;
}
public static ILogger<T> GetRequiredLogger<T>( this IServiceProvider provider ) {
_ = provider ?? throw new ArgumentNullException(nameof(provider));
return provider.GetRequiredService<ILogger<T>>();
}
public static bool TryGetService<TS>( this IServiceProvider provider, [NotNullWhen(true)] out TS? service ) where TS : class {
_ = provider ?? throw new ArgumentNullException(nameof(provider));
object? fetch = provider.GetService(typeof(TS));
if (fetch is TS cast) {
service = cast;
return true;
}
service = null;
return false;
}
}
}