-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ServiceProviderAccessor to allow access to IServiceProvider
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/Temporalio.Extensions.Hosting/IServiceProviderAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Temporalio.Extensions.Hosting/ServiceProviderAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
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(); | ||
|
||
/// <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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters