-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceProvider.cs
More file actions
100 lines (95 loc) · 3.76 KB
/
ServiceProvider.cs
File metadata and controls
100 lines (95 loc) · 3.76 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using Microsoft.Extensions.DependencyInjection;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Diagnostics;
namespace RapidField.SolidInstruments.InversionOfControl
{
/// <summary>
/// Resolves service objects.
/// </summary>
public sealed class ServiceProvider : IServiceProvider, ISupportRequiredService
{
/// <summary>
/// Initializes a new instance of the <see cref="ServiceProvider" /> class.
/// </summary>
/// <param name="container">
/// A dependency container that resolves services for the provider.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="container" /> is <see langword="null" />.
/// </exception>
[DebuggerHidden]
internal ServiceProvider(IDependencyContainer container)
{
Container = container.RejectIf().IsNull(nameof(container)).TargetArgument;
}
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">
/// An object that specifies the type of service object to get.
/// </param>
/// <returns>
/// A service object of type <paramref name="serviceType" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="serviceType" /> is <see langword="null" />.
/// </exception>
/// <exception cref="DependencyResolutionException">
/// An exception was raised while attempting to resolve the dependency.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public Object GetRequiredService(Type serviceType)
{
try
{
return Container.Resolve(serviceType);
}
catch (DependencyResolutionException)
{
throw;
}
catch (Exception exception)
{
throw new DependencyResolutionException(serviceType, exception);
}
}
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">
/// An object that specifies the type of service object to get.
/// </param>
/// <returns>
/// A service object of type <paramref name="serviceType" /> -or- <see langword="null" /> if there is no service object of
/// type <paramref name="serviceType" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="serviceType" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public Object GetService(Type serviceType)
{
try
{
return GetRequiredService(serviceType);
}
catch (DependencyResolutionException)
{
return null;
}
}
/// <summary>
/// Represents a dependency container that resolves services for the provider.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly IDependencyContainer Container;
}
}