Skip to content

Fix snapstart scope issue#2253

Merged
GarrettBeatty merged 2 commits intodevfrom
fixsnapstartscope
Jan 12, 2026
Merged

Fix snapstart scope issue#2253
GarrettBeatty merged 2 commits intodevfrom
fixsnapstartscope

Conversation

@GarrettBeatty
Copy link
Copy Markdown
Contributor

@GarrettBeatty GarrettBeatty commented Jan 12, 2026

Issue #, if available: #2236

Issue

When using AddAWSLambdaBeforeSnapshotRequest with authentication middleware (or any middleware using scoped services), SnapStart warmup requests fail with InvalidOperationException: Cannot resolve scoped service 'IAuthenticationHandlerProvider' from root provider. This only occurs when ASPNETCORE_ENVIRONMENT=Development.

Root Cause

The AddRegisterBeforeSnapshot method in AbstractAspNetCoreFunction.cs was setting RequestServices directly to the root IServiceProvider instead of creating a service scope. In Development mode, ASP.NET Core validates that scoped services aren't resolved from the root provider and throws an exception.

Fix

Create a service scope before processing each warmup request, matching the behavior of the normal request handler (FunctionHandlerAsync):

Testing

Deployed lambda with snapstart before bug fix and confirmed error

2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 info: Microsoft.AspNetCore.Hosting.Diagnostics[1]  
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 Request starting  GET https:/// - - 0
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 An unhandled exception has occurred while executing the request.
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 System.InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider' from root provider.
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(ServiceCallSite callSite, IServiceScope scope, IServiceScope rootScope)
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2026-01-12T17:12:47.534000+00:00 2026/01/12/[1]f18cf865861842deafb0920ce2588c76 at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

deployed same lambda with bug fix and confirmed no error

2026-01-12T17:19:07.873000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e INIT_START Runtime Version: dotnet:8.v71        Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:eb460407d38dfe2ec5e81b083a5ed55d92546b9b21bc76680359038583d98730
2026-01-12T17:19:08.507000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
2026-01-12T17:19:08.507000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Using an in-memory repository. Keys will not be persisted to storage.
2026-01-12T17:19:08.507000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
2026-01-12T17:19:08.507000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
2026-01-12T17:19:08.566000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]
2026-01-12T17:19:08.566000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Creating key {2f9bc182-868a-4d20-86d6-2ec6fcc3c5bf} with creation date 2026-01-12 17:19:08Z, activation date 2026-01-12 17:19:08Z, and expiration date 2026-04-12 17:19:08Z.
2026-01-12T17:19:08.617000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
2026-01-12T17:19:08.617000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e No XML encryptor configured. Key {2f9bc182-868a-4d20-86d6-2ec6fcc3c5bf} may be persisted to storage in unencrypted form.
2026-01-12T17:19:08.926000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction[0]
2026-01-12T17:19:08.926000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request does not contain domain name information but is derived from APIGatewayProxyFunction.
2026-01-12T17:19:08.967000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[1]  
2026-01-12T17:19:08.967000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request starting  GET https:/// - - 0
2026-01-12T17:19:09.153000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
2026-01-12T17:19:09.153000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executing endpoint 'HTTP: GET /'
2026-01-12T17:19:09.164000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
2026-01-12T17:19:09.164000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executed endpoint 'HTTP: GET /'
2026-01-12T17:19:09.187000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[2]  
2026-01-12T17:19:09.187000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request finished  GET https:/// - 200 - text/plain;+charset=utf-8 220.6285ms
2026-01-12T17:19:09.187000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction[0]
2026-01-12T17:19:09.187000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request does not contain domain name information but is derived from APIGatewayProxyFunction.
2026-01-12T17:19:09.188000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[1]  
2026-01-12T17:19:09.188000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request starting  GET https:/// - - 0
2026-01-12T17:19:09.188000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
2026-01-12T17:19:09.188000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executing endpoint 'HTTP: GET /'
2026-01-12T17:19:09.188000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
2026-01-12T17:19:09.188000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executed endpoint 'HTTP: GET /'
2026-01-12T17:19:09.203000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[2]  
2026-01-12T17:19:09.203000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request finished  GET https:/// - 200 - text/plain;+charset=utf-8 0.9351ms
2026-01-12T17:19:09.204000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction[0]
2026-01-12T17:19:09.204000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request does not contain domain name information but is derived from APIGatewayProxyFunction.
2026-01-12T17:19:09.204000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[1]  
2026-01-12T17:19:09.204000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request starting  GET https:/// - - 0
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executing endpoint 'HTTP: GET /'
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request starting  GET https:/// - - 0
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executing endpoint 'HTTP: GET /'
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executed endpoint 'HTTP: GET /'
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request finished  GET https:/// - 200 - text/plain;+charset=utf-8 0.0623ms   
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e warn: Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction[0]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request does not contain domain name information but is derived from APIGatewayProxyFunction.
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request starting  GET https:/// - - 0
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executing endpoint 'HTTP: GET /'
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Executed endpoint 'HTTP: GET /'
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
2026-01-12T17:19:09.205000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e Request finished  GET https:/// - 200 - text/plain;+charset=utf-8 0.0614ms   
2026-01-12T17:19:09.367000+00:00 2026/01/12/[2]e5b6d949771a4741ae0a1a5bba3bd13e INIT_REPORT Init Duration: 1494.21 ms

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

{
"Projects": [
{
"Name": "Amazon.Lambda.AspNetCoreServer",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autover wasnt working so i added both of these. also why do we need both hosting and server in the changefile? i saw that was done for another PR that touched this class

@GarrettBeatty GarrettBeatty marked this pull request as ready for review January 12, 2026 17:55
@GarrettBeatty GarrettBeatty merged commit 561eadc into dev Jan 12, 2026
9 checks passed
@GarrettBeatty GarrettBeatty deleted the fixsnapstartscope branch January 12, 2026 18:59
@philasmar philasmar restored the fixsnapstartscope branch January 12, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants