Skip to content

Commit

Permalink
Add optional parameter to set custom serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
James Eastham committed Sep 15, 2022
1 parent 0e8e42a commit e39962d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public APIGatewayHttpApiV2LambdaRuntimeSupportServer(IServiceProvider servicePro
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new APIGatewayHttpApiV2MinimalApi(serviceProvider).FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, new DefaultLambdaJsonSerializer());
return HandlerWrapper.GetHandlerWrapper(handler, Utilities.Serializer);
}

/// <summary>
Expand Down Expand Up @@ -111,7 +111,7 @@ public APIGatewayRestApiLambdaRuntimeSupportServer(IServiceProvider serviceProvi
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new APIGatewayRestApiMinimalApi(serviceProvider).FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, new DefaultLambdaJsonSerializer());
return HandlerWrapper.GetHandlerWrapper(handler, Utilities.Serializer);
}

/// <summary>
Expand Down Expand Up @@ -152,7 +152,7 @@ public ApplicationLoadBalancerLambdaRuntimeSupportServer(IServiceProvider servic
protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider)
{
var handler = new ApplicationLoadBalancerMinimalApi(serviceProvider).FunctionHandlerAsync;
return HandlerWrapper.GetHandlerWrapper(handler, new DefaultLambdaJsonSerializer());
return HandlerWrapper.GetHandlerWrapper(handler, Utilities.Serializer);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Amazon.Lambda.AspNetCoreServer.Internal;
using Amazon.Lambda.AspNetCoreServer.Hosting.Internal;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -35,8 +37,23 @@ public static class ServiceCollectionExtensions
/// </summary>
/// <param name="services"></param>
/// <param name="eventSource"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection services, LambdaEventSource eventSource)
{
// Not running in Lambda so exit and let Kestrel be the web server
return services.AddAWSLambdaHosting(eventSource, new DefaultLambdaJsonSerializer());
}

/// <summary>
/// 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.
/// </summary>
/// <param name="services"></param>
/// <param name="eventSource"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection services, LambdaEventSource eventSource, ILambdaSerializer serializer)
{
// Not running in Lambda so exit and let Kestrel be the web server
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME")))
Expand All @@ -50,6 +67,8 @@ public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection ser
_ => throw new ArgumentException($"Event source type {eventSource} unknown")
};


Utilities.Serializer = serializer;
Utilities.EnsureLambdaServerRegistered(services, serverType);
return services;
}
Expand Down
10 changes: 10 additions & 0 deletions Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;

namespace Amazon.Lambda.AspNetCoreServer.Internal
{
Expand All @@ -22,6 +24,14 @@ namespace Amazon.Lambda.AspNetCoreServer.Internal
/// </summary>
public static class Utilities
{
private static ILambdaSerializer _serializer;

public static ILambdaSerializer Serializer
{
get { return _serializer ??= new DefaultLambdaJsonSerializer(); }
set => _serializer = value;
}

public static void EnsureLambdaServerRegistered(IServiceCollection services)
{
EnsureLambdaServerRegistered(services, typeof(LambdaServer));
Expand Down

0 comments on commit e39962d

Please sign in to comment.