-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceScope.cs
More file actions
85 lines (79 loc) · 3.29 KB
/
ServiceScope.cs
File metadata and controls
85 lines (79 loc) · 3.29 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
// =================================================================================================================================
// 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;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Diagnostics;
using System.Threading;
namespace RapidField.SolidInstruments.InversionOfControl
{
/// <summary>
/// Abstracts <see cref="IDependencyScope" /> for use by <see cref="IServiceCollection" /> instances.
/// </summary>
public sealed class ServiceScope : Instrument, IServiceScope
{
/// <summary>
/// Initializes a new instance of the <see cref="ServiceScope" /> class.
/// </summary>
/// <param name="dependencyScope">
/// The scope that is abstracted by the service scope.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="dependencyScope" /> is <see langword="null" />.
/// </exception>
public ServiceScope(IDependencyScope dependencyScope)
{
DependencyScope = dependencyScope.RejectIf().IsNull(nameof(dependencyScope)).TargetArgument;
LazyProvider = new Lazy<IServiceProvider>(() => DependencyScope.Resolve<IServiceProvider>(), LazyThreadSafetyMode.ExecutionAndPublication);
}
/// <summary>
/// Releases all resources consumed by the current <see cref="ServiceScope" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing)
{
try
{
if (disposing)
{
DependencyScope.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Gets the service provider that is used to resolve dependencies for the scope.
/// </summary>
/// <exception cref="DependencyResolutionException">
/// An exception was raised while attempting to resolve the service provider.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public IServiceProvider ServiceProvider
{
get
{
RejectIfDisposed();
return LazyProvider.Value;
}
}
/// <summary>
/// Represents the scope that is abstracted by the service scope.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IDependencyScope DependencyScope;
/// <summary>
/// Represents the lazily-initialized service provider that is used to resolve dependencies for the scope.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<IServiceProvider> LazyProvider;
}
}