internal async Task InvokeOnceAsync(CancellationToken cancellationToken = default) { using (var invocation = await Client.GetNextInvocationAsync(cancellationToken)) { InvocationResponse response = null; bool invokeSucceeded = false; try { response = await _handler(invocation); invokeSucceeded = true; } catch (Exception exception) { await Client.ReportInvocationErrorAsync(invocation.LambdaContext.AwsRequestId, exception); } if (invokeSucceeded) { try { await Client.SendResponseAsync(invocation.LambdaContext.AwsRequestId, response?.OutputStream); } finally { if (response != null && response.DisposeOutputStream) { response.OutputStream?.Dispose(); } } } } }
/// <summary> /// Get a HandlerWrapper that will call the given delegate on function invocation. /// </summary> /// <param name="invokeDelegate">Action called for each invocation of the Lambda function</param> /// <returns>A HandlerWrapper</returns> public static HandlerWrapper GetHandlerWrapper(Action <Stream, ILambdaContext, MemoryStream> invokeDelegate) { var handlerWrapper = new HandlerWrapper(); handlerWrapper.Handler = invocation => { handlerWrapper.OutputStream.SetLength(0); invokeDelegate(invocation.InputStream, invocation.LambdaContext, handlerWrapper.OutputStream); handlerWrapper.OutputStream.Position = 0; var response = new InvocationResponse(handlerWrapper.OutputStream, false); return(Task.FromResult(response)); }; return(handlerWrapper); }
internal async Task InvokeOnceAsync(CancellationToken cancellationToken = default) { this._logger.LogInformation($"Starting InvokeOnceAsync"); using (var invocation = await Client.GetNextInvocationAsync(cancellationToken)) { InvocationResponse response = null; bool invokeSucceeded = false; try { this._logger.LogInformation($"Starting invoking handler"); response = await _handler(invocation); invokeSucceeded = true; } catch (Exception exception) { WriteUnhandledExceptionToLog(exception); await Client.ReportInvocationErrorAsync(invocation.LambdaContext.AwsRequestId, exception); } finally { this._logger.LogInformation($"Finished invoking handler"); } if (invokeSucceeded) { this._logger.LogInformation($"Starting sending response"); try { await Client.SendResponseAsync(invocation.LambdaContext.AwsRequestId, response?.OutputStream); } finally { if (response != null && response.DisposeOutputStream) { response.OutputStream?.Dispose(); } this._logger.LogInformation($"Finished sending response"); } } this._logger.LogInformation($"Finished InvokeOnceAsync"); } }