Skip to content

Commit

Permalink
Evolve to only make IServiceProvider accessible to InterceptActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
tdg5 committed Dec 9, 2024
1 parent b657fe0 commit 79c2d00
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 69 deletions.
16 changes: 0 additions & 16 deletions src/Temporalio.Extensions.Hosting/IServiceProviderAccessor.cs

This file was deleted.

42 changes: 0 additions & 42 deletions src/Temporalio.Extensions.Hosting/ServiceProviderAccessor.cs

This file was deleted.

13 changes: 2 additions & 11 deletions src/Temporalio.Extensions.Hosting/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,7 @@ public static ActivityDefinition CreateTemporalActivityDefinition(
#else
var scope = provider.CreateScope();
#endif
IServiceProviderAccessor? serviceProviderAccessor =
scope.ServiceProvider.GetService<IServiceProviderAccessor>();

if (serviceProviderAccessor is not null)
{
serviceProviderAccessor.ServiceProvider = scope.ServiceProvider;
}
ActivityServiceProviderAccessor.AsyncLocalCurrent.Value = scope.ServiceProvider;

try
{
Expand Down Expand Up @@ -119,10 +113,7 @@ public static ActivityDefinition CreateTemporalActivityDefinition(
}
finally
{
if (serviceProviderAccessor is not null)
{
serviceProviderAccessor.ServiceProvider = null;
}
ActivityServiceProviderAccessor.AsyncLocalCurrent.Value = null;
#if NET6_0_OR_GREATER
await scope.DisposeAsync().ConfigureAwait(false);
#else
Expand Down
31 changes: 31 additions & 0 deletions src/Temporalio/Activities/ActivityServiceProviderAccessor.cs
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");
}
}
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
}
}

0 comments on commit 79c2d00

Please sign in to comment.