Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore default request service feature after middleware completes #2686

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,45 @@ public async Task ServiceProviders_Equal()
var middleware = new WorkerRequestServicesMiddleware(next, httpCoordinator.Object);
await middleware.Invoke(httpContext);
}

private class MyService

[Fact]
public async Task DefaultServiceProvider_Restored()
{
var services = new ServiceCollection();
services.AddScoped<MyService>();

var httpContextProvider = services.BuildServiceProvider().CreateScope().ServiceProvider;

// FunctionContext services will be scoped.
var functionContextProvider = services.BuildServiceProvider().CreateScope().ServiceProvider;

RequestDelegate next = (ctxt) =>
{
Assert.Same(functionContextProvider, ctxt.RequestServices);

return Task.CompletedTask;
};

var functionContext = new TestFunctionContext(new TestFunctionDefinition(), new TestFunctionInvocation(), CancellationToken.None,
serviceProvider: functionContextProvider);

var httpContext = new DefaultHttpContext();
httpContext.RequestServices = httpContextProvider;
httpContext.Request.Headers.Add(Constants.CorrelationHeader, functionContext.InvocationId);

var httpCoordinator = new Mock<IHttpCoordinator>();
httpCoordinator
.Setup(p => p.SetHttpContextAsync(functionContext.InvocationId, httpContext))
.ReturnsAsync(functionContext);

var middleware = new WorkerRequestServicesMiddleware(next, httpCoordinator.Object);
await middleware.Invoke(httpContext);

Assert.Same(httpContextProvider, httpContext.RequestServices);
Assert.NotSame(functionContextProvider, httpContext.RequestServices);
}

private class MyService;
}
}

Loading