Skip to content
Merged
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 @@ -37,8 +37,8 @@ public abstract class APIGatewayProxyFunction
/// </summary>
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
Expand Down Expand Up @@ -80,7 +80,26 @@ public abstract class APIGatewayProxyFunction
/// <summary>
/// Default constructor that AWS Lambda will invoke.
/// </summary>
protected APIGatewayProxyFunction()
protected APIGatewayProxyFunction(bool autoStart = true)
{
if (autoStart)
{
Start();
}
}

private bool IsStarted
{
get
{
return _server != null;
}
}

/// <summary>
/// Should be called in the derived constructor
/// </summary>
protected void Start()
{
var builder = CreateWebHostBuilder();
Init(builder);
Expand All @@ -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.");
}
Expand Down Expand Up @@ -173,6 +192,11 @@ public virtual async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatew
{
lambdaContext.Logger.LogLine($"Incoming {request.HttpMethod} requests to {request.Path}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You are missing an invocation to a new virtual method that you could override to set the environment variables.

  1. You need a new virtual PreStart method that takes in the ILambdaContext. I wouldn't pass in the request since this method would only be invoked the on the first request, and we want to prevent misuse of this method.

  2. You need to call this method in the !IsStarted if statement. Since this method will only be useful for altering the properties of the environment before the service starts, we should isolate this call so it doesn't cause inadvertent side effects.

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}");
Expand Down