1010using Amazon . Lambda . AspNetCoreServer . Internal ;
1111
1212using Microsoft . AspNetCore . Hosting ;
13+ using Microsoft . AspNetCore . Hosting . Internal ;
1314using Microsoft . AspNetCore . Http . Features ;
1415
1516using Newtonsoft . Json ;
@@ -23,13 +24,13 @@ namespace Amazon.Lambda.AspNetCoreServer
2324 /// </summary>
2425 public abstract class APIGatewayProxyFunction
2526 {
26- IWebHost _host ;
27- APIGatewayServer _server ;
27+ private readonly IWebHost _host ;
28+ private readonly APIGatewayServer _server ;
2829
2930 /// <summary>
3031 /// Default constructor that AWS Lambda will invoke.
3132 /// </summary>
32- public APIGatewayProxyFunction ( )
33+ protected APIGatewayProxyFunction ( )
3334 {
3435 var builder = new WebHostBuilder ( ) ;
3536 Init ( builder ) ;
@@ -71,23 +72,41 @@ public APIGatewayProxyFunction()
7172 public virtual async Task < APIGatewayProxyResponse > FunctionHandlerAsync ( APIGatewayProxyRequest request , ILambdaContext lambdaContext )
7273 {
7374 lambdaContext ? . Logger . Log ( $ "Incoming { request . HttpMethod } requests to { request . Path } ") ;
74-
7575 InvokeFeatures features = new InvokeFeatures ( ) ;
7676 MarshallRequest ( features , request ) ;
77+ var context = this . CreateContext ( features ) ;
78+ return await this . ProcessRequest ( lambdaContext , context , features ) ;
79+ }
80+
81+ /// <summary>
82+ /// Creates a <see cref="HostingApplication.Context"/> object using the <see cref="APIGatewayServer"/> field in the class.
83+ /// </summary>
84+ /// <param name="features"><see cref="IFeatureCollection"/> implementation.</param>
85+ protected HostingApplication . Context CreateContext ( IFeatureCollection features )
86+ {
87+ return _server . Application . CreateContext ( features ) ;
88+ }
7789
78- var context = _server . Application . CreateContext ( features ) ;
90+ /// <summary>
91+ /// Processes the current request.
92+ /// </summary>
93+ /// <param name="lambdaContext"><see cref="ILambdaContext"/> implementation.</param>
94+ /// <param name="context">The hosting application request context object.</param>
95+ /// <param name="features">An <see cref="InvokeFeatures"/> instance.</param>
96+ protected async Task < APIGatewayProxyResponse > ProcessRequest ( ILambdaContext lambdaContext , HostingApplication . Context context , InvokeFeatures features )
97+ {
7998 try
8099 {
81100 await this . _server . Application . ProcessRequestAsync ( context ) ;
82101 this . _server . Application . DisposeContext ( context , null ) ;
83102 }
84103 catch ( Exception e )
85104 {
86- lambdaContext ? . Logger . Log ( $ "Unknown error responding to request: { ErrorReport ( e ) } ") ;
105+ lambdaContext ? . Logger . Log ( $ "Unknown error responding to request: { this . ErrorReport ( e ) } ") ;
87106 this . _server . Application . DisposeContext ( context , e ) ;
88107 }
89108
90- var response = MarshallResponse ( features ) ;
109+ var response = this . MarshallResponse ( features ) ;
91110
92111 // ASP.NET Core Web API does not always set the status code if the request was
93112 // successful
@@ -119,7 +138,7 @@ private string ErrorReport(Exception e)
119138 /// </summary>
120139 /// <param name="requestFeatures"></param>
121140 /// <param name="apiGatewayRequest"></param>
122- private void MarshallRequest ( IHttpRequestFeature requestFeatures , APIGatewayProxyRequest apiGatewayRequest )
141+ protected void MarshallRequest ( IHttpRequestFeature requestFeatures , APIGatewayProxyRequest apiGatewayRequest )
123142 {
124143 requestFeatures . Scheme = "https" ;
125144 requestFeatures . Path = apiGatewayRequest . Path ;
0 commit comments