Skip to content

Commit 2297048

Browse files
committed
Update the Minimal API extension method to use GetBeforeSnapshotRequests.
1 parent 71540b3 commit 2297048

13 files changed

+111
-309
lines changed

.autover/changes/38c5bace-4ca5-4f83-8094-ae6d912ca20a.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Amazon.Lambda.AspNetCoreServer.Hosting",
55
"Type": "Patch",
66
"ChangelogMessages": [
7-
"Add AddAWSLambdaBeforeSnapshotRequest to support warming up the asp.net/lambda pipelines automatically during BeforeSnapshot callback."
7+
"Add overrideable method GetBeforeSnapshotRequests() and AddAWSLambdaBeforeSnapshotRequest() extension method to support warming up the asp.net/lambda pipelines automatically during BeforeSnapshot callback."
88
]
99
}
1010
]

Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Amazon.Lambda.AspNetCoreServer.Hosting.csproj

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
<ProjectReference Include="..\Amazon.Lambda.APIGatewayEvents\Amazon.Lambda.APIGatewayEvents.csproj" />
2828
<ProjectReference Include="..\Amazon.Lambda.AspNetCoreServer\Amazon.Lambda.AspNetCoreServer.csproj" />
2929
<ProjectReference Include="..\Amazon.Lambda.RuntimeSupport\Amazon.Lambda.RuntimeSupport.csproj" />
30-
<ProjectReference Include="..\Amazon.Lambda.Serialization.SystemTextJson\Amazon.Lambda.Serialization.SystemTextJson.csproj" />
31-
32-
</ItemGroup>
30+
</ItemGroup>
3331

3432
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Amazon.Lambda.AspNetCoreServer.Hosting.Internal;
7+
8+
/// <summary>
9+
/// Helper class for storing Requests for
10+
/// <see cref="ServiceCollectionExtensions.AddAWSLambdaBeforeSnapshotRequest(IServiceCollection, Func{IEnumerable{HttpRequestMessage}})"/>
11+
/// </summary>
12+
internal class GetBeforeSnapshotRequestsCollector
13+
{
14+
public Func<IEnumerable<HttpRequestMessage>> Requests { get; set; }
15+
}
16+

Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Internal/LambdaRuntimeSupportServer.cs

+48-14
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public abstract class LambdaRuntimeSupportServer : LambdaServer
1717
{
1818
private readonly IServiceProvider _serviceProvider;
1919

20-
#if NET8_0_OR_GREATER
21-
private readonly LambdaSnapstartExecuteRequestsBeforeSnapshotHelper _snapstartInitHelper;
22-
#endif
23-
2420
internal ILambdaSerializer Serializer;
2521

2622
/// <summary>
@@ -31,10 +27,6 @@ public LambdaRuntimeSupportServer(IServiceProvider serviceProvider)
3127
{
3228
_serviceProvider = serviceProvider;
3329

34-
#if NET8_0_OR_GREATER
35-
_snapstartInitHelper = _serviceProvider.GetRequiredService<LambdaSnapstartExecuteRequestsBeforeSnapshotHelper>();
36-
#endif
37-
3830
Serializer = serviceProvider.GetRequiredService<ILambdaSerializer>();
3931
}
4032

@@ -51,12 +43,6 @@ public override Task StartAsync<TContext>(IHttpApplication<TContext> application
5143

5244
var handlerWrapper = CreateHandlerWrapper(_serviceProvider);
5345

54-
#if NET8_0_OR_GREATER
55-
56-
_snapstartInitHelper.RegisterInitializerRequests(handlerWrapper);
57-
58-
#endif
59-
6046
var bootStrap = new LambdaBootstrap(handlerWrapper);
6147
return bootStrap.RunAsync();
6248
}
@@ -99,14 +85,30 @@ protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceP
9985
/// </summary>
10086
public class APIGatewayHttpApiV2MinimalApi : APIGatewayHttpApiV2ProxyFunction
10187
{
88+
private readonly IEnumerable<GetBeforeSnapshotRequestsCollector> _beforeSnapshotRequestsCollectors;
89+
10290
/// <summary>
10391
/// Create instances
10492
/// </summary>
10593
/// <param name="serviceProvider">The IServiceProvider created for the ASP.NET Core application</param>
10694
public APIGatewayHttpApiV2MinimalApi(IServiceProvider serviceProvider)
10795
: base(serviceProvider)
10896
{
97+
_beforeSnapshotRequestsCollectors = serviceProvider.GetServices<GetBeforeSnapshotRequestsCollector>();
10998
}
99+
100+
#if NET8_0_OR_GREATER
101+
protected override IEnumerable<HttpRequestMessage> GetBeforeSnapshotRequests()
102+
{
103+
foreach (var collector in _beforeSnapshotRequestsCollectors)
104+
{
105+
foreach (var req in collector.Requests())
106+
{
107+
yield return req;
108+
}
109+
}
110+
}
111+
#endif
110112
}
111113
}
112114

@@ -140,14 +142,30 @@ protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceP
140142
/// </summary>
141143
public class APIGatewayRestApiMinimalApi : APIGatewayProxyFunction
142144
{
145+
private readonly IEnumerable<GetBeforeSnapshotRequestsCollector> _beforeSnapshotRequestsCollectors;
146+
143147
/// <summary>
144148
/// Create instances
145149
/// </summary>
146150
/// <param name="serviceProvider">The IServiceProvider created for the ASP.NET Core application</param>
147151
public APIGatewayRestApiMinimalApi(IServiceProvider serviceProvider)
148152
: base(serviceProvider)
149153
{
154+
_beforeSnapshotRequestsCollectors = serviceProvider.GetServices<GetBeforeSnapshotRequestsCollector>();
155+
}
156+
157+
#if NET8_0_OR_GREATER
158+
protected override IEnumerable<HttpRequestMessage> GetBeforeSnapshotRequests()
159+
{
160+
foreach (var collector in _beforeSnapshotRequestsCollectors)
161+
{
162+
foreach (var req in collector.Requests())
163+
{
164+
yield return req;
165+
}
166+
}
150167
}
168+
#endif
151169
}
152170
}
153171

@@ -181,14 +199,30 @@ protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceP
181199
/// </summary>
182200
public class ApplicationLoadBalancerMinimalApi : ApplicationLoadBalancerFunction
183201
{
202+
private readonly IEnumerable<GetBeforeSnapshotRequestsCollector> _beforeSnapshotRequestsCollectors;
203+
184204
/// <summary>
185205
/// Create instances
186206
/// </summary>
187207
/// <param name="serviceProvider">The IServiceProvider created for the ASP.NET Core application</param>
188208
public ApplicationLoadBalancerMinimalApi(IServiceProvider serviceProvider)
189209
: base(serviceProvider)
190210
{
211+
_beforeSnapshotRequestsCollectors = serviceProvider.GetServices<GetBeforeSnapshotRequestsCollector>();
212+
}
213+
214+
#if NET8_0_OR_GREATER
215+
protected override IEnumerable<HttpRequestMessage> GetBeforeSnapshotRequests()
216+
{
217+
foreach (var collector in _beforeSnapshotRequestsCollectors)
218+
{
219+
foreach (var req in collector.Requests())
220+
{
221+
yield return req;
222+
}
223+
}
191224
}
225+
#endif
192226
}
193227
}
194228
}

Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Internal/LambdaSnapstartExecuteRequestsBeforeSnapshotHelper.cs

-142
This file was deleted.

Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection ser
135135
public static IServiceCollection AddAWSLambdaBeforeSnapshotRequest(this IServiceCollection services, Func<IEnumerable<HttpRequestMessage>> beforeSnapStartRequests)
136136
{
137137
#if NET8_0_OR_GREATER
138-
LambdaSnapstartExecuteRequestsBeforeSnapshotHelper.Registrar.Register(beforeSnapStartRequests);
138+
services.AddSingleton(new GetBeforeSnapshotRequestsCollector
139+
{
140+
Requests = beforeSnapStartRequests
141+
});
142+
139143
#endif
140144

141145
return services;
@@ -174,11 +178,6 @@ private static bool TryLambdaSetup(IServiceCollection services, LambdaEventSourc
174178

175179
Utilities.EnsureLambdaServerRegistered(services, serverType);
176180

177-
#if NET8_0_OR_GREATER
178-
// register a LambdaSnapStartInitializerHttpMessageHandler
179-
services.AddSingleton<LambdaSnapstartExecuteRequestsBeforeSnapshotHelper>(new LambdaSnapstartExecuteRequestsBeforeSnapshotHelper(eventSource));
180-
#endif
181-
182181
return true;
183182
}
184183
}

0 commit comments

Comments
 (0)