Skip to content

Commit 3ef6931

Browse files
committed
Added SimpleInjectorAddOptions.ServiceScopeFactory property. Fixes #18
1 parent aa551b1 commit 3ef6931

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

src/SimpleInjector.Integration.ServiceCollection/SimpleInjectorAddOptions.cs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,8 @@ internal SimpleInjectorAddOptions(
4444
/// <exception cref="ArgumentNullException">Thrown when a null value is provided.</exception>
4545
public IServiceProviderAccessor ServiceProviderAccessor
4646
{
47-
get
48-
{
49-
return this.serviceProviderAccessor;
50-
}
51-
52-
set
53-
{
54-
if (value is null)
55-
{
56-
throw new ArgumentNullException(nameof(value));
57-
}
58-
59-
this.serviceProviderAccessor = value;
60-
}
47+
get => this.serviceProviderAccessor;
48+
set => this.serviceProviderAccessor = value ?? throw new ArgumentNullException(nameof(value));
6149
}
6250

6351
/// <summary>
@@ -79,29 +67,28 @@ public IServiceProviderAccessor ServiceProviderAccessor
7967
/// <value>A boolean value.</value>
8068
public bool DisposeContainerWithServiceProvider { get; set; } = true;
8169

70+
/// <summary>
71+
/// Gets the framework's <see cref="IServiceScopeFactory"/> instance from the <see cref="ApplicationServices"/>.
72+
/// It's value will be set when
73+
/// <see cref="SimpleInjectorServiceCollectionExtensions.UseSimpleInjector(IServiceProvider, Container)">UseSimpleInjector</see>
74+
/// is called, or when ASP.NET Core resolves its hosted services (whatever comes first).
75+
/// </summary>
76+
/// <exception cref="InvalidOperationException">Thrown when the property has been called while ita value hasn't
77+
/// been set yet.</exception>
78+
public IServiceScopeFactory ServiceScopeFactory =>
79+
this.serviceScopeFactory ??= this.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
80+
8281
/// <summary>
8382
/// Gets the <see cref="IServiceProvider"/> instance that will be used by Simple Injector to resolve
8483
/// cross-wired framework components. It's value will be set when
8584
/// <see cref="SimpleInjectorServiceCollectionExtensions.UseSimpleInjector(IServiceProvider, Container)">UseSimpleInjector</see>
8685
/// is called, or when ASP.NET Core resolves its hosted services (whatever comes first).
8786
/// </summary>
87+
/// <exception cref="InvalidOperationException">Thrown when the property has been called while ita value hasn't
88+
/// been set yet.</exception>
8889
/// <value>The <see cref="IServiceProvider"/> instance.</value>
89-
public IServiceProvider ApplicationServices
90-
{
91-
get
92-
{
93-
if (this.applicationServices is null)
94-
{
95-
throw this.GetServiceProviderNotSetException();
96-
}
97-
98-
return this.applicationServices;
99-
}
100-
}
101-
102-
internal IServiceScopeFactory ServiceScopeFactory =>
103-
this.serviceScopeFactory
104-
?? (this.serviceScopeFactory = this.GetRequiredFrameworkService<IServiceScopeFactory>());
90+
public IServiceProvider ApplicationServices =>
91+
this.applicationServices ?? throw this.GetServiceProviderNotSetException();
10592

10693
internal T GetRequiredFrameworkService<T>() => this.ApplicationServices.GetRequiredService<T>();
10794

0 commit comments

Comments
 (0)