Skip to content

Commit

Permalink
Implement the save and restore including proper disposal of the reque…
Browse files Browse the repository at this point in the history
…st service feature
  • Loading branch information
danielmarbach committed Oct 16, 2024
1 parent 129ae3f commit 99dc2a5
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ public async Task Invoke(HttpContext context)

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

// Retrieve the existing service provider feature
var existingFeature = context.Features.Get<IServiceProvidersFeature>();

// Explicitly set the RequestServices to prevent a new scope from being created internally.
// This also prevents the scope from being disposed when the request is complete. We want this to
// be disposed in the Functions middleware, not here.
var servicesFeature = new RequestServicesFeature(context, null)
await using var servicesFeature = new RequestServicesFeature(context, null)
{
RequestServices = functionContext.InstanceServices
};
context.Features.Set<IServiceProvidersFeature>(servicesFeature);

await _next(context);
try
{
await _next(context);
}
finally
{
context.Features.Set(existingFeature);
}
}
}

0 comments on commit 99dc2a5

Please sign in to comment.