diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs
index 6bfbb0ae4..80483b02d 100644
--- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs
+++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs
@@ -37,8 +37,8 @@ public abstract class APIGatewayProxyFunction
///
public const string APIGATEWAY_REQUEST = "APIGatewayRequest";
- private readonly IWebHost _host;
- private readonly APIGatewayServer _server;
+ private IWebHost _host;
+ private APIGatewayServer _server;
// Defines a mapping from registered content types to the response encoding format
// which dictates what transformations should be applied before returning response content
@@ -80,7 +80,26 @@ public abstract class APIGatewayProxyFunction
///
/// Default constructor that AWS Lambda will invoke.
///
- protected APIGatewayProxyFunction()
+ protected APIGatewayProxyFunction(bool autoStart = true)
+ {
+ if (autoStart)
+ {
+ Start();
+ }
+ }
+
+ private bool IsStarted
+ {
+ get
+ {
+ return _server != null;
+ }
+ }
+
+ ///
+ /// Should be called in the derived constructor
+ ///
+ protected void Start()
{
var builder = CreateWebHostBuilder();
Init(builder);
@@ -90,7 +109,7 @@ protected APIGatewayProxyFunction()
_host.Start();
_server = _host.Services.GetService(typeof(Microsoft.AspNetCore.Hosting.Server.IServer)) as APIGatewayServer;
- if(_server == null)
+ if (_server == null)
{
throw new Exception("Failed to find the implementation APIGatewayServer for the IServer registration. This can happen if UseApiGateway was not called.");
}
@@ -173,6 +192,11 @@ public virtual async Task FunctionHandlerAsync(APIGatew
{
lambdaContext.Logger.LogLine($"Incoming {request.HttpMethod} requests to {request.Path}");
+ if (!IsStarted)
+ {
+ Start();
+ }
+
InvokeFeatures features = new InvokeFeatures();
MarshallRequest(features, request, lambdaContext);
lambdaContext.Logger.LogLine($"ASP.NET Core Request PathBase: {((IHttpRequestFeature)features).PathBase}, Path: {((IHttpRequestFeature)features).Path}");