-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add ServiceProviderAccessor to allow access to IServiceProvider from Interceptor #364
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace Temporalio.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Provides access to the current, scoped <see cref="IServiceProvider"/> if | ||
/// one is available. | ||
/// </summary> | ||
public interface IServiceProviderAccessor | ||
{ | ||
/// <summary> | ||
/// Gets or sets the current service provider. | ||
/// </summary> | ||
IServiceProvider? ServiceProvider { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adapted from ASP.NET's |
||
using System.Threading; | ||
|
||
namespace Temporalio.Extensions.Hosting | ||
{ | ||
/// <summary> | ||
/// Provides an implementation of <see cref="IServiceProvider" /> based on | ||
/// the current execution context. | ||
/// </summary> | ||
public class ServiceProviderAccessor : IServiceProviderAccessor | ||
{ | ||
private static readonly AsyncLocal<ServiceProviderHolder> ServiceProviderCurrent = new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
|
||
/// <inheritdoc/> | ||
public IServiceProvider? ServiceProvider | ||
{ | ||
get => ServiceProviderCurrent.Value?.ServiceProvider; | ||
|
||
set | ||
{ | ||
var holder = ServiceProviderCurrent.Value; | ||
if (holder != null) | ||
{ | ||
// Clear current IServiceProvider trapped in the AsyncLocals, as its done. | ||
holder.ServiceProvider = null; | ||
} | ||
|
||
if (value != null) | ||
{ | ||
// Use an object indirection to hold the IServiceProvider in the AsyncLocal, | ||
// so it can be cleared in all ExecutionContexts when its cleared. | ||
ServiceProviderCurrent.Value = new ServiceProviderHolder { ServiceProvider = value }; | ||
} | ||
} | ||
} | ||
|
||
private sealed class ServiceProviderHolder | ||
{ | ||
public IServiceProvider? ServiceProvider { get; set; } | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,14 @@ public static ActivityDefinition CreateTemporalActivityDefinition( | |
#else | ||
var scope = provider.CreateScope(); | ||
#endif | ||
IServiceProviderAccessor? serviceProviderAccessor = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inspiration taken from ASP.NET's |
||
scope.ServiceProvider.GetService<IServiceProviderAccessor>(); | ||
|
||
if (serviceProviderAccessor is not null) | ||
{ | ||
serviceProviderAccessor.ServiceProvider = scope.ServiceProvider; | ||
} | ||
|
||
try | ||
{ | ||
object? result; | ||
|
@@ -111,6 +119,10 @@ public static ActivityDefinition CreateTemporalActivityDefinition( | |
} | ||
finally | ||
{ | ||
if (serviceProviderAccessor is not null) | ||
{ | ||
serviceProviderAccessor.ServiceProvider = null; | ||
} | ||
#if NET6_0_OR_GREATER | ||
await scope.DisposeAsync().ConfigureAwait(false); | ||
#else | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adapted from ASP.NET's
IHttpContextAccessor
.