-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathHttpContextAccessorCurrentMessageBusProvider.cs
More file actions
48 lines (39 loc) · 1.64 KB
/
HttpContextAccessorCurrentMessageBusProvider.cs
File metadata and controls
48 lines (39 loc) · 1.64 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
namespace SlimMessageBus.Host;
/// <summary>
/// Resolves the <see cref="IMessageBus"/> from the current ASP.NET Core web request (if present, otherwise falls back to the application root container).
/// </summary>
public partial class HttpContextAccessorCurrentMessageBusProvider(
ILogger<HttpContextAccessorCurrentMessageBusProvider> logger,
IHttpContextAccessor httpContextAccessor,
IServiceProvider serviceProvider)
: CurrentMessageBusProvider(serviceProvider)
{
private readonly ILogger<HttpContextAccessorCurrentMessageBusProvider> _logger = logger;
public override IMessageBus GetCurrent()
{
// When the call to resolve the given type is made within an HTTP Request, use the request scope service provider
var httpContext = httpContextAccessor?.HttpContext;
if (httpContext != null)
{
LogCurrentFrom("request");
return httpContext.RequestServices.GetService<IMessageBus>();
}
// otherwise use the app wide scope provider
LogCurrentFrom("root");
return base.GetCurrent();
}
#region Logging
[LoggerMessage(
EventId = 0,
Level = LogLevel.Trace,
Message = "The type IMessageBus will be requested from the {ScopeName} scope")]
private partial void LogCurrentFrom(string scopeName);
#endregion
}
#if NETSTANDARD2_0
public partial class HttpContextAccessorCurrentMessageBusProvider
{
private partial void LogCurrentFrom(string scopeName)
=> _logger.LogTrace("The type IMessageBus will be requested from the {ScopeName} scope", scopeName);
}
#endif