Skip to content

Commit fba3fac

Browse files
committed
Added ServiceScopeProvider class which is registered by .AddSimpleInjector and allows replacing the registered IServiceScope. Fixes #14
1 parent aa551b1 commit fba3fac

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/SimpleInjector.Integration.ServiceCollection/DefaultServiceProviderAccessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ internal DefaultServiceProviderAccessor(Container container)
1616
this.container = container;
1717
}
1818

19-
public IServiceProvider Current => this.CurentServiceScope.ServiceProvider;
19+
public IServiceProvider Current => this.CurrentServiceScope.ServiceProvider;
2020

21-
private IServiceScope CurentServiceScope
21+
private IServiceScope CurrentServiceScope
2222
{
2323
get
2424
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Simple Injector Contributors. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE file in the project root for license information.
3+
4+
namespace SimpleInjector
5+
{
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
/// <summary>
9+
/// Allows providing an <see cref="IServiceScope"/> that can be used within a single Simple Injector
10+
/// <see cref="Scope"/>. When the <see cref="ServiceScope"/> property is set before an
11+
/// <see cref="IServiceScope"/> is resolved for the first time within a <see cref="Scope"/>, the provided value
12+
/// will be returned. When this property is set <i>after</i> <see cref="IServiceScope"/> is resolved, it has
13+
/// no effect.
14+
/// </summary>
15+
/// <remarks>
16+
/// <code lang="cs"><![CDATA[
17+
/// using (AsyncScopedLifestyle.BeginScope(container))
18+
/// {
19+
/// container.GetInstance<ServiceScopeProvider>().ServiceScope = serviceScope;
20+
///
21+
/// var resolvedScope = container.GetInstance<IServiceScope>();
22+
///
23+
/// Assert.AreSame(serviceScope, resolvedScope);
24+
/// }
25+
/// ]]></code>
26+
/// </remarks>
27+
public sealed class ServiceScopeProvider
28+
{
29+
/// <summary>
30+
/// Gets or sets the <see cref="IServiceScope"/>.
31+
/// </summary>
32+
public IServiceScope? ServiceScope { get; set; }
33+
}
34+
35+
}

src/SimpleInjector.Integration.ServiceCollection/SimpleInjectorServiceCollectionExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,13 @@ private static void VerifyStringLocalizerFactoryAvailable(IServiceCollection ser
374374

375375
private static void RegisterServiceScope(SimpleInjectorAddOptions options)
376376
{
377-
options.Container.Register(
378-
() => options.ServiceScopeFactory.CreateScope(),
377+
var container = options.Container;
378+
379+
container.Register<ServiceScopeProvider>(Lifestyle.Scoped);
380+
381+
container.Register(
382+
() => container.GetInstance<ServiceScopeProvider>().ServiceScope
383+
?? options.ServiceScopeFactory.CreateScope(),
379384
Lifestyle.Scoped);
380385
}
381386

0 commit comments

Comments
 (0)