Skip to content

Commit 561eadc

Browse files
Fix snapstart scope issue (#2253)
* Fix snapstart scope issue * add dev config
1 parent 9ea4482 commit 561eadc

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.AspNetCoreServer",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Fix scoped service resolution in SnapStart warmup requests"
8+
]
9+
},
10+
{
11+
"Name": "Amazon.Lambda.AspNetCoreServer.Hosting",
12+
"Type": "Patch",
13+
"ChangelogMessages": [
14+
"Fix scoped service resolution in SnapStart warmup requests"
15+
]
16+
}
17+
]
18+
}

Libraries/src/Amazon.Lambda.AspNetCoreServer/AbstractAspNetCoreFunction.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,14 @@ private void AddRegisterBeforeSnapshot()
320320

321321
for (var i = 0; i < invokeTimes; i++)
322322
{
323+
// Create a scope to properly handle scoped services (e.g., IAuthenticationHandlerProvider)
324+
// This matches the behavior in FunctionHandlerAsync and prevents InvalidOperationException
325+
// when resolving scoped services in Development mode where scope validation is enabled.
326+
using var scope = _hostServices.CreateScope();
327+
323328
InvokeFeatures features = new InvokeFeatures();
324329
(features as IItemsFeature).Items = new Dictionary<object, object>();
325-
(features as IServiceProvidersFeature).RequestServices = _hostServices;
330+
(features as IServiceProvidersFeature).RequestServices = scope.ServiceProvider;
326331

327332
MarshallRequest(features, request, new SnapStartEmptyLambdaContext());
328333

0 commit comments

Comments
 (0)