using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; namespace Amazon.Lambda.AspNetCoreServer.Internal { /// /// Implements the ASP.NET Core IServer interface and exposes the application object for the Lambda function /// to initiate a web request. /// public class LambdaServer : IServer { /// /// The application is used by the Lambda function to initiate a web request through the ASP.NET Core framework. /// public ApplicationWrapper Application { get; set; } public IFeatureCollection Features { get; } = new FeatureCollection(); public void Dispose() { } public virtual Task StartAsync(IHttpApplication application, CancellationToken cancellationToken) { this.Application = new ApplicationWrapper(application); return Task.CompletedTask; } public virtual Task StopAsync(CancellationToken cancellationToken) { return Task.CompletedTask; } public abstract class ApplicationWrapper { internal abstract object CreateContext(IFeatureCollection features); internal abstract Task ProcessRequestAsync(object context); internal abstract void DisposeContext(object context, Exception exception); } public class ApplicationWrapper : ApplicationWrapper, IHttpApplication { private readonly IHttpApplication _application; public ApplicationWrapper(IHttpApplication application) { _application = application; } internal override object CreateContext(IFeatureCollection features) { return ((IHttpApplication)this).CreateContext(features); } TContext IHttpApplication.CreateContext(IFeatureCollection features) { return _application.CreateContext(features); } internal override void DisposeContext(object context, Exception exception) { ((IHttpApplication)this).DisposeContext((TContext)context, exception); } void IHttpApplication.DisposeContext(TContext context, Exception exception) { _application.DisposeContext(context, exception); } internal override Task ProcessRequestAsync(object context) { return ((IHttpApplication)this).ProcessRequestAsync((TContext)context); } Task IHttpApplication.ProcessRequestAsync(TContext context) { return _application.ProcessRequestAsync(context); } } } }