-
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.
Evolve to only make IServiceProvider accessible to InterceptActivity
- Loading branch information
Showing
5 changed files
with
64 additions
and
69 deletions.
There are no files selected for viewing
16 changes: 0 additions & 16 deletions
16
src/Temporalio.Extensions.Hosting/IServiceProviderAccessor.cs
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
src/Temporalio.Extensions.Hosting/ServiceProviderAccessor.cs
This file was deleted.
Oops, something went wrong.
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
31 changes: 31 additions & 0 deletions
31
src/Temporalio/Activities/ActivityServiceProviderAccessor.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,31 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Temporalio.Activities | ||
{ | ||
/// <summary> | ||
/// Provides access to the scoped <see cref="IServiceProvider" /> for the | ||
/// current activity execution context. | ||
/// </summary> | ||
public static class ActivityServiceProviderAccessor | ||
{ | ||
/// <summary> | ||
/// Gets the async local current value. | ||
/// </summary> | ||
public static readonly AsyncLocal<IServiceProvider?> AsyncLocalCurrent = new(); | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the current code is running in an | ||
/// activity with a service provider. | ||
/// </summary> | ||
public static bool HasCurrent => AsyncLocalCurrent.Value is not null; | ||
|
||
/// <summary> | ||
/// Gets the current activity's scoped <see cref="IServiceProvider"/>. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">If no <see | ||
/// cref="IServiceProvider"/> is available.</exception> | ||
public static IServiceProvider Current => AsyncLocalCurrent.Value ?? | ||
throw new InvalidOperationException("No current service provider"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Temporalio/Worker/Interceptors/IWorkerInterceptorWithServiceProvider.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,31 @@ | ||
using System; | ||
#if NETCOREAPP3_0_OR_GREATER | ||
using Temporalio.Activities; | ||
#endif | ||
|
||
namespace Temporalio.Worker.Interceptors | ||
{ | ||
/// <summary> | ||
/// Interceptor for intercepting activities and workflows providing activity | ||
/// interceptors with access to the service provider. | ||
/// </summary> | ||
public interface IWorkerInterceptorWithServiceProvider : IWorkerInterceptor | ||
{ | ||
/// <summary> | ||
/// Create an activity inbound interceptor to intercept calls, with | ||
/// access to the service provider for the current activity. | ||
/// </summary> | ||
/// <param name="serviceProvider">Service provider instance scoped to the current activity.</param> | ||
/// <param name="nextInterceptor">The next interceptor in the chain to call.</param> | ||
/// <returns>Created interceptor.</returns> | ||
#if NETCOREAPP3_0_OR_GREATER | ||
ActivityInboundInterceptor InterceptActivity(IServiceProvider serviceProvider, ActivityInboundInterceptor nextInterceptor) => | ||
nextInterceptor; | ||
|
||
ActivityInboundInterceptor InterceptActivity(ActivityInboundInterceptor nextInterceptor) => | ||
InterceptActivity(ActivityServiceProviderAccessor.Current, nextInterceptor); | ||
#else | ||
ActivityInboundInterceptor InterceptActivity(IServiceProvider serviceProvider, ActivityInboundInterceptor nextInterceptor); | ||
#endif | ||
} | ||
} |