diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Amazon.Lambda.AspNetCoreServer.Hosting.csproj b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Amazon.Lambda.AspNetCoreServer.Hosting.csproj index aadcc21f7..81cedb167 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Amazon.Lambda.AspNetCoreServer.Hosting.csproj +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Amazon.Lambda.AspNetCoreServer.Hosting.csproj @@ -4,20 +4,25 @@ Package for running ASP.NET Core applications using the Minimal API style as a AWS Lambda function. - net6.0 + net6.0;net8.0 enable enable - 1.6.1 + 1.7.0 README.md Amazon.Lambda.AspNetCoreServer.Hosting Amazon.Lambda.AspNetCoreServer.Hosting + + IL2104,IL2026,IL2067,IL2075 + true + true - + + diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs index c68ccc81d..82fa10376 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs @@ -4,6 +4,8 @@ using Amazon.Lambda.Core; using Amazon.Lambda.Serialization.SystemTextJson; using Microsoft.Extensions.DependencyInjection.Extensions; +using System.Text.Json.Serialization; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Extensions.DependencyInjection { @@ -40,6 +42,7 @@ public static class ServiceCollectionExtensions /// /// /// + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("For Native AOT the overload passing in a SourceGeneratorLambdaJsonSerializer instance must be used to avoid reflection with JSON serialization.")] public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection services, LambdaEventSource eventSource) { // Not running in Lambda so exit and let Kestrel be the web server @@ -54,19 +57,50 @@ public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection ser /// /// /// + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("For Native AOT the overload passing in a SourceGeneratorLambdaJsonSerializer instance must be used to avoid reflection with JSON serialization.")] public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection services, LambdaEventSource eventSource, Action? configure = null) { + if (TryLambdaSetup(services, eventSource, configure, out var hostingOptions)) + { + services.TryAddSingleton(hostingOptions!.Serializer ?? new DefaultLambdaJsonSerializer()); + } + + return services; + } + + /// + /// Add the ability to run the ASP.NET Core Lambda function in AWS Lambda. If the project is not running in Lambda + /// this method will do nothing allowing the normal Kestrel webserver to host the application. + /// + /// + /// + /// + /// + /// + public static IServiceCollection AddAWSLambdaHosting<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this IServiceCollection services, LambdaEventSource eventSource, SourceGeneratorLambdaJsonSerializer serializer, Action? configure = null) + where T : JsonSerializerContext + { + if(TryLambdaSetup(services, eventSource, configure, out var hostingOptions)) + { + services.TryAddSingleton(serializer ?? hostingOptions!.Serializer); + } + + return services; + } + + private static bool TryLambdaSetup(IServiceCollection services, LambdaEventSource eventSource, Action? configure, out HostingOptions? hostingOptions) + { + hostingOptions = null; + // Not running in Lambda so exit and let Kestrel be the web server if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME"))) - return services; + return false; + + hostingOptions = new HostingOptions(); - var hostingOptions = new HostingOptions(); - if (configure != null) configure.Invoke(hostingOptions); - services.TryAddSingleton(hostingOptions.Serializer ?? new DefaultLambdaJsonSerializer()); - var serverType = eventSource switch { LambdaEventSource.HttpApi => typeof(APIGatewayHttpApiV2LambdaRuntimeSupportServer), @@ -74,10 +108,10 @@ public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection ser LambdaEventSource.ApplicationLoadBalancer => typeof(ApplicationLoadBalancerLambdaRuntimeSupportServer), _ => throw new ArgumentException($"Event source type {eventSource} unknown") }; - + Utilities.EnsureLambdaServerRegistered(services, serverType); - - return services; + + return true; } } } diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction.cs index abec4a701..a7bcd519d 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Net; using System.Security.Claims; -using System.Text; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.Logging; @@ -13,7 +12,6 @@ using Amazon.Lambda.AspNetCoreServer.Internal; using Microsoft.AspNetCore.Http.Features.Authentication; using System.Globalization; -using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.Primitives; using Microsoft.Net.Http.Headers; diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction{TStartup}.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction{TStartup}.cs index 67b7f7d47..a05d9da4c 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction{TStartup}.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayHttpApiV2ProxyFunction{TStartup}.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Hosting; +using System.Diagnostics.CodeAnalysis; namespace Amazon.Lambda.AspNetCoreServer { @@ -8,7 +9,7 @@ namespace Amazon.Lambda.AspNetCoreServer /// the Lambda function will point to this base class FunctionHandlerAsync method. /// /// The type containing the startup methods for the application. - public abstract class APIGatewayHttpApiV2ProxyFunction : APIGatewayHttpApiV2ProxyFunction where TStartup : class + public abstract class APIGatewayHttpApiV2ProxyFunction<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] TStartup> : APIGatewayHttpApiV2ProxyFunction where TStartup : class { /// /// Default Constructor. The ASP.NET Core Framework will be initialized as part of the construction. diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction{TStartup}.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction{TStartup}.cs index bdf9344c7..93d28c78d 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction{TStartup}.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction{TStartup}.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Hosting; +using System.Diagnostics.CodeAnalysis; namespace Amazon.Lambda.AspNetCoreServer { @@ -8,7 +9,7 @@ namespace Amazon.Lambda.AspNetCoreServer /// the Lambda function will point to this base class FunctionHandlerAsync method. /// /// The type containing the startup methods for the application. - public abstract class APIGatewayProxyFunction : APIGatewayProxyFunction where TStartup : class + public abstract class APIGatewayProxyFunction<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] TStartup> : APIGatewayProxyFunction where TStartup : class { /// /// Default Constructor. The ASP.NET Core Framework will be initialized as part of the construction. diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/AbstractAspNetCoreFunction.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/AbstractAspNetCoreFunction.cs index 9ba95feb0..a6bb02484 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/AbstractAspNetCoreFunction.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/AbstractAspNetCoreFunction.cs @@ -206,60 +206,6 @@ public void RegisterResponseContentEncodingForContentEncoding(string contentEnco /// protected virtual void Init(IWebHostBuilder builder) { } - /// - /// Creates the IWebHostBuilder similar to WebHost.CreateDefaultBuilder but replacing the registration of the Kestrel web server with a - /// registration for Lambda. - /// - /// - [Obsolete("Functions should migrate to CreateHostBuilder and use IHostBuilder to setup their ASP.NET Core application. In a future major version update of this library support for IWebHostBuilder will be removed for non .NET Core 2.1 Lambda functions.")] - protected virtual IWebHostBuilder CreateWebHostBuilder() - { - var builder = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .ConfigureAppConfiguration((hostingContext, config) => - { - var env = hostingContext.HostingEnvironment; - - config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); - - if (env.IsDevelopment()) - { - var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); - if (appAssembly != null) - { - config.AddUserSecrets(appAssembly, optional: true); - } - } - - config.AddEnvironmentVariables(); - }) - .ConfigureLogging((hostingContext, logging) => - { - logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); - - if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("LAMBDA_TASK_ROOT"))) - { - logging.AddConsole(); - logging.AddDebug(); - } - else - { - logging.AddLambdaLogger(hostingContext.Configuration, "Logging"); - } - }) - .UseDefaultServiceProvider((hostingContext, options) => - { - options.ValidateScopes = hostingContext.HostingEnvironment.IsDevelopment(); - }); - - Init(builder); - - // Swap out Kestrel as the webserver and use our implementation of IServer - builder.UseLambdaServer(); - - return builder; - } /// /// Method to initialize the host builder before starting the host. In a typical Web API this is similar to the main function. @@ -317,34 +263,17 @@ private protected bool IsStarted /// protected void Start() { - // For .NET Core 3.1 and above use the IHostBuilder instead of IWebHostBuilder used in .NET Core 2.1. If the user overrode CreateWebHostBuilder - // then fallback to the original .NET Core 2.1 behavior. - if (this.GetType().GetMethod("CreateWebHostBuilder", BindingFlags.NonPublic | BindingFlags.Instance).DeclaringType.FullName.StartsWith("Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction")) - { - var builder = CreateHostBuilder(); - builder.ConfigureServices(services => - { - Utilities.EnsureLambdaServerRegistered(services); - }); - - var host = builder.Build(); - PostCreateHost(host); - - host.Start(); - this._hostServices = host.Services; - } - else + var builder = CreateHostBuilder(); + builder.ConfigureServices(services => { -#pragma warning disable 618 - var builder = CreateWebHostBuilder(); -#pragma warning restore 618 + Utilities.EnsureLambdaServerRegistered(services); + }); - var host = builder.Build(); - PostCreateWebHost(host); + var host = builder.Build(); + PostCreateHost(host); - host.Start(); - this._hostServices = host.Services; - } + host.Start(); + this._hostServices = host.Services; _server = this._hostServices.GetService(typeof(Microsoft.AspNetCore.Hosting.Server.IServer)) as LambdaServer; if (_server == null) diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/Amazon.Lambda.AspNetCoreServer.csproj b/Libraries/src/Amazon.Lambda.AspNetCoreServer/Amazon.Lambda.AspNetCoreServer.csproj index e3fcf80c2..2d718d23d 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/Amazon.Lambda.AspNetCoreServer.csproj +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/Amazon.Lambda.AspNetCoreServer.csproj @@ -4,19 +4,23 @@ Amazon.Lambda.AspNetCoreServer makes it easy to run ASP.NET Core Web API applications as AWS Lambda functions. - netcoreapp3.1;net6.0 + net6.0;net8.0 Amazon.Lambda.AspNetCoreServer - 8.1.1 + 9.0.0 Amazon.Lambda.AspNetCoreServer Amazon.Lambda.AspNetCoreServer AWS;Amazon;Lambda;aspnetcore Latest false - README.md + README.md + + IL2026,IL2067,IL2075,IL2091 + true + true - + diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/ApplicationLoadBalancerFunction{TStartup}.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/ApplicationLoadBalancerFunction{TStartup}.cs index 61dfc392a..bda533314 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/ApplicationLoadBalancerFunction{TStartup}.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/ApplicationLoadBalancerFunction{TStartup}.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Hosting; +using System.Diagnostics.CodeAnalysis; namespace Amazon.Lambda.AspNetCoreServer { @@ -8,7 +9,7 @@ namespace Amazon.Lambda.AspNetCoreServer /// the Lambda function will point to this base class FunctionHandlerAsync method. /// /// The type containing the startup methods for the application. - public abstract class ApplicationLoadBalancerFunction : ApplicationLoadBalancerFunction where TStartup : class + public abstract class ApplicationLoadBalancerFunction<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] TStartup> : ApplicationLoadBalancerFunction where TStartup : class { /// /// Default Constructor. The ASP.NET Core Framework will be initialized as part of the construction. diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/Utilities.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/Utilities.cs index c8d50a6a3..e3b76bd5b 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/Utilities.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/Utilities.cs @@ -14,6 +14,7 @@ using System.Threading; using System.Threading.Tasks; using Amazon.Lambda.APIGatewayEvents; +using System.Diagnostics.CodeAnalysis; namespace Amazon.Lambda.AspNetCoreServer.Internal { @@ -22,12 +23,21 @@ namespace Amazon.Lambda.AspNetCoreServer.Internal /// public static class Utilities { + /// + /// Method to make sure the Lambda implementation is the only registered implementaiton of IServer for ASP.NET Core runtime. + /// + /// public static void EnsureLambdaServerRegistered(IServiceCollection services) { EnsureLambdaServerRegistered(services, typeof(LambdaServer)); } - public static void EnsureLambdaServerRegistered(IServiceCollection services, Type serverType) + /// + /// Method to make sure the Lambda implementation is the only registered implementaiton of IServer for ASP.NET Core runtime. + /// + /// + /// + public static void EnsureLambdaServerRegistered(IServiceCollection services, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type serverType) { IList toRemove = new List(); var serviceDescriptions = services.Where(x => x.ServiceType == typeof(IServer)); diff --git a/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/Amazon.Lambda.Serialization.SystemTextJson.csproj b/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/Amazon.Lambda.Serialization.SystemTextJson.csproj index 774936116..d1f98e0d1 100644 --- a/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/Amazon.Lambda.Serialization.SystemTextJson.csproj +++ b/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/Amazon.Lambda.Serialization.SystemTextJson.csproj @@ -9,7 +9,7 @@ Amazon.Lambda.Serialization.SystemTextJson Amazon.Lambda.Serialization.SystemTextJson AWS;Amazon;Lambda - 2.4.0 + 2.4.1 README.md diff --git a/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/SourceGeneratorLambdaJsonSerializer.cs b/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/SourceGeneratorLambdaJsonSerializer.cs index 0d763f300..a97cb49c0 100644 --- a/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/SourceGeneratorLambdaJsonSerializer.cs +++ b/Libraries/src/Amazon.Lambda.Serialization.SystemTextJson/SourceGeneratorLambdaJsonSerializer.cs @@ -95,6 +95,7 @@ public SourceGeneratorLambdaJsonSerializer(Action customi /// /// /// + [Obsolete("The method is marked obsolete because the jsonSerializerContext parameter is unlikely to be created with the required JsonSerializerOptions for Lambda serialization. This will trigger confusing NullReferenceException.")] public SourceGeneratorLambdaJsonSerializer(TSGContext jsonSerializerContext, Action customizer = null, Action jsonWriterCustomizer = null) : base(jsonWriterCustomizer) { diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/Amazon.Lambda.AspNetCoreServer.Test.csproj b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/Amazon.Lambda.AspNetCoreServer.Test.csproj index 91c75ea37..0ec5b957c 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/Amazon.Lambda.AspNetCoreServer.Test.csproj +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/Amazon.Lambda.AspNetCoreServer.Test.csproj @@ -1,7 +1,7 @@  - net6.0 + net6.0;net8.0 Amazon.Lambda.AspNetCoreServer.Test Library Amazon.Lambda.AspNetCoreServer.Test diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestWhichBuilderIsUsed.cs b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestWhichBuilderIsUsed.cs deleted file mode 100644 index 5b81a3e35..000000000 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestWhichBuilderIsUsed.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Net; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -using Amazon.Lambda.APIGatewayEvents; -using Amazon.Lambda.TestUtilities; - -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -using TestWebApp; - -using Xunit; - -namespace Amazon.Lambda.AspNetCoreServer.Test -{ - - public class TestWhichBuilderIsUsed - { - [Theory] - [InlineData(typeof(HostBuilderUsingGenericClass), true)] - [InlineData(typeof(HostBuilderOverridingInit), true)] - [InlineData(typeof(HostBuilderOverridingCreateWebHostBuilder), false)] - [InlineData(typeof(HostBuilderOverridingCreateHostBuilder), true)] - [InlineData(typeof(HostBuilderOverridingInitHostBuilderAndCallsConfigureWebHostDefaults), true)] - [InlineData(typeof(HostBuilderOverridingInitHostBuilderAndCallsConfigureWebHostLambdaDefaults), true)] - public async Task TestUsingGenericBaseClass(Type functionType, bool initHostCalled) - { - var methodsCalled = await InvokeAPIGatewayRequestWithContent(functionType); - Assert.Equal(initHostCalled, methodsCalled.InitHostBuilder); - - Assert.True(methodsCalled.InitHostWebBuilder); - } - - private async Task InvokeAPIGatewayRequestWithContent(Type functionType) - { - var context = new TestLambdaContext(); - - var filePath = Path.Combine(Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location), "values-get-all-apigateway-request.json"); - var requestContent = File.ReadAllText(filePath); - - var lambdaFunction = Activator.CreateInstance(functionType) as APIGatewayProxyFunction; - var requestStream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(requestContent)); - var request = new Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer().Deserialize(requestStream); - var response = await lambdaFunction.FunctionHandlerAsync(request, context); - Assert.Equal(200, response.StatusCode); - return lambdaFunction as IMethodsCalled; - } - } -} diff --git a/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/Amazon.Lambda.RuntimeSupport.UnitTests.csproj b/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/Amazon.Lambda.RuntimeSupport.UnitTests.csproj index fb5219ab3..943c29ada 100644 --- a/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/Amazon.Lambda.RuntimeSupport.UnitTests.csproj +++ b/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/Amazon.Lambda.RuntimeSupport.UnitTests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net8.0 diff --git a/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/HandlerTests.cs b/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/HandlerTests.cs index 67881b443..db99d880b 100644 --- a/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/HandlerTests.cs +++ b/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/HandlerTests.cs @@ -383,9 +383,7 @@ private async Task ExecHandlerAsync(string handler, string dataIn using (var actionWriter = new StringWriter()) { var testRuntimeApiClient = new TestRuntimeApiClient(_environmentVariables, _headers); - var loggerAction = actionWriter.ToLoggingAction(); - var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(UserCodeLoader.LambdaCoreAssemblyName)); - UserCodeLoader.SetCustomerLoggerLogAction(assembly, loggerAction, _internalLogger); + var userCodeLoader = new UserCodeLoader(handler, _internalLogger); var handlerWrapper = HandlerWrapper.GetHandlerWrapper(userCodeLoader.Invoke); @@ -395,16 +393,20 @@ private async Task ExecHandlerAsync(string handler, string dataIn Client = testRuntimeApiClient }; + var loggerAction = actionWriter.ToLoggingAction(); + var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(UserCodeLoader.LambdaCoreAssemblyName)); + UserCodeLoader.SetCustomerLoggerLogAction(assembly, loggerAction, _internalLogger); + if (assertLoggedByInitialize != null) { - Assert.False(actionWriter.ToString().Contains($"^^[{assertLoggedByInitialize}]^^")); + Assert.DoesNotContain($"^^[{assertLoggedByInitialize}]^^", actionWriter.ToString()); } await bootstrap.InitializeAsync(); if (assertLoggedByInitialize != null) { - Assert.True(actionWriter.ToString().Contains($"^^[{assertLoggedByInitialize}]^^")); + Assert.Contains($"^^[{assertLoggedByInitialize}]^^", actionWriter.ToString()); } var dataOut = await InvokeAsync(bootstrap, dataIn, testRuntimeApiClient); diff --git a/Libraries/test/HandlerTest/HandlerTest.csproj b/Libraries/test/HandlerTest/HandlerTest.csproj index 540214175..8d6628c76 100644 --- a/Libraries/test/HandlerTest/HandlerTest.csproj +++ b/Libraries/test/HandlerTest/HandlerTest.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + net8.0 false ../../build/private/test/HandlerTest/$(Configuration) diff --git a/Libraries/test/HandlerTestNoSerializer/HandlerTestNoSerializer.csproj b/Libraries/test/HandlerTestNoSerializer/HandlerTestNoSerializer.csproj index 79abb6637..21c25bf34 100644 --- a/Libraries/test/HandlerTestNoSerializer/HandlerTestNoSerializer.csproj +++ b/Libraries/test/HandlerTestNoSerializer/HandlerTestNoSerializer.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + net8.0 false ../../build/private/test/HandlerTestNoSerializer/$(Configuration) diff --git a/Libraries/test/TestWebApp/HostBuilderTestClasses.cs b/Libraries/test/TestWebApp/HostBuilderTestClasses.cs deleted file mode 100644 index d2ee3c253..000000000 --- a/Libraries/test/TestWebApp/HostBuilderTestClasses.cs +++ /dev/null @@ -1,144 +0,0 @@ -using Amazon.Lambda.AspNetCoreServer; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; - -#if !NETCOREAPP_2_1 -namespace TestWebApp -{ - public interface IMethodsCalled - { - bool InitHostBuilder { get; set; } - bool InitHostWebBuilder { get; set; } - } - - public class HostBuilderUsingGenericClass : APIGatewayProxyFunction, IMethodsCalled - { - public bool InitHostBuilder { get; set; } - public bool InitHostWebBuilder { get; set; } - - protected override void Init(IWebHostBuilder builder) - { - base.Init(builder); - InitHostWebBuilder = true; - } - - protected override void Init(IHostBuilder builder) - { - base.Init(builder); - InitHostBuilder = true; - } - } - - public class HostBuilderOverridingInit : APIGatewayProxyFunction, IMethodsCalled - { - public bool InitHostBuilder { get; set; } - public bool InitHostWebBuilder { get; set; } - - protected override void Init(IWebHostBuilder builder) - { - builder.UseStartup(); - - InitHostWebBuilder = true; - } - - protected override void Init(IHostBuilder builder) - { - base.Init(builder); - InitHostBuilder = true; - } - } - - public class HostBuilderOverridingCreateWebHostBuilder : APIGatewayProxyFunction, IMethodsCalled - { - public bool InitHostBuilder { get; set; } - public bool InitHostWebBuilder { get; set; } - - protected override IWebHostBuilder CreateWebHostBuilder() - { - return base.CreateWebHostBuilder(); - } - - protected override void Init(IWebHostBuilder builder) - { - builder.UseStartup(); - InitHostWebBuilder = true; - } - - protected override void Init(IHostBuilder builder) - { - base.Init(builder); - InitHostBuilder = true; - } - } - - public class HostBuilderOverridingCreateHostBuilder : APIGatewayProxyFunction, IMethodsCalled - { - public bool InitHostBuilder { get; set; } - public bool InitHostWebBuilder { get; set; } - - protected override IHostBuilder CreateHostBuilder() - { - return base.CreateHostBuilder(); - } - - protected override void Init(IWebHostBuilder builder) - { - builder.UseStartup(); - InitHostWebBuilder = true; - } - - protected override void Init(IHostBuilder builder) - { - base.Init(builder); - InitHostBuilder = true; - } - } - - - public class HostBuilderOverridingInitHostBuilderAndCallsConfigureWebHostDefaults : APIGatewayProxyFunction, IMethodsCalled - { - public bool InitHostBuilder { get; set; } - public bool InitHostWebBuilder { get; set; } - - - protected override void Init(IWebHostBuilder builder) - { - InitHostWebBuilder = true; - } - - protected override void Init(IHostBuilder builder) - { - InitHostBuilder = true; - - builder - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } - } - - public class HostBuilderOverridingInitHostBuilderAndCallsConfigureWebHostLambdaDefaults : APIGatewayProxyFunction, IMethodsCalled - { - public bool InitHostBuilder { get; set; } - public bool InitHostWebBuilder { get; set; } - - - protected override void Init(IWebHostBuilder builder) - { - InitHostWebBuilder = true; - } - - protected override void Init(IHostBuilder builder) - { - InitHostBuilder = true; - - builder - .ConfigureWebHostLambdaDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } - } -} -#endif \ No newline at end of file diff --git a/Libraries/test/TestWebApp/TestWebApp.csproj b/Libraries/test/TestWebApp/TestWebApp.csproj index 93a5de397..e5607beb2 100644 --- a/Libraries/test/TestWebApp/TestWebApp.csproj +++ b/Libraries/test/TestWebApp/TestWebApp.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0;net8.0 true TestWebApp Exe