Skip to content

Commit 99dc2a5

Browse files
committed
Implement the save and restore including proper disposal of the request service feature
1 parent 129ae3f commit 99dc2a5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/WorkerRequestServicesMiddleware.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,25 @@ public async Task Invoke(HttpContext context)
2727

2828
FunctionContext functionContext = await _coordinator.SetHttpContextAsync(invocationId, context);
2929

30+
// Retrieve the existing service provider feature
31+
var existingFeature = context.Features.Get<IServiceProvidersFeature>();
32+
3033
// Explicitly set the RequestServices to prevent a new scope from being created internally.
3134
// This also prevents the scope from being disposed when the request is complete. We want this to
3235
// be disposed in the Functions middleware, not here.
33-
var servicesFeature = new RequestServicesFeature(context, null)
36+
await using var servicesFeature = new RequestServicesFeature(context, null)
3437
{
3538
RequestServices = functionContext.InstanceServices
3639
};
3740
context.Features.Set<IServiceProvidersFeature>(servicesFeature);
3841

39-
await _next(context);
42+
try
43+
{
44+
await _next(context);
45+
}
46+
finally
47+
{
48+
context.Features.Set(existingFeature);
49+
}
4050
}
4151
}

0 commit comments

Comments
 (0)