-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AspNetCoreHttpScopeProvider.cs
27 lines (24 loc) · 1.05 KB
/
AspNetCoreHttpScopeProvider.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
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using SteroidsDI.Core;
namespace SteroidsDI.AspNetCore;
/// <summary>
/// <see cref="IScopeProvider"/> for ASP.NET Core working with <see cref="IHttpContextAccessor"/>.
/// </summary>
public sealed class AspNetCoreHttpScopeProvider : IScopeProvider
{
/// <summary>Gets scoped <see cref="IServiceProvider" /> for the current HTTP request.</summary>
/// <param name="rootProvider">The root <see cref="IServiceProvider" /> object to obtain <see cref="IHttpContextAccessor"/>.</param>
/// <returns>The scoped <see cref="IServiceProvider" /> object or <c>null</c> if there is no current HTTP request.</returns>
public IServiceProvider? GetScopedServiceProvider(IServiceProvider rootProvider)
{
var accessor = rootProvider.GetService<IHttpContextAccessor>();
if (accessor != null)
{
var context = accessor.HttpContext;
if (context != null)
return context.RequestServices;
}
return null;
}
}