Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SourceGeneratorLambdaJsonSerializer documentation for AddAWSLambdaHosting is not clear #1817

Open
petro2050 opened this issue Sep 21, 2024 · 2 comments
Labels
documentation This is a problem with documentation. module/lambda-client-lib needs-reproduction This issue needs reproduction. p2 This is a standard priority issue

Comments

@petro2050
Copy link

Describe the issue

After turning on PublishTrimmed, I noticed this line is throwing an error:

builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

Using member 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddAWSLambdaHosting(IServiceCollection, LambdaEventSource)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. For Native AOT the overload passing in a SourceGeneratorLambdaJsonSerializer instance must be used to avoid reflection with JSON serialization.

In another case, the instructions were clear:

await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<MyContext>()).Build().RunAsync();

See this summary. I was able to provide a custom context with the required request/response types.

So, in the first case, how should we provide a custom context?

  • Is there a default context we can use?
  • How should the serialization options be configured if we want backwards compatibility with the non-trimmed code?
  • Do we need to register every request/response object (used in controllers) manually?

Links

SourceGeneratorLambdaJsonSerializer

@petro2050 petro2050 added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Sep 21, 2024
@bhoradc bhoradc added investigating This issue is being investigated and/or work is in progress to resolve the issue. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Sep 23, 2024
@normj
Copy link
Member

normj commented Sep 27, 2024

The error message is saying you need to use the overload of AddAWSLambdaHosting to pass in the context like the following. The name of the method isn't in the actual error message because the message is being given for that method call. Would it have helped if we did put AddAWSLambdaHosting explicitly in the error message?

builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi, new SourceGeneratorLambdaJsonSerializer<LambdaJsonContext>());

@petro2050
Copy link
Author

@normj I understand we need to pass in the context. My question was more about the details of the context itself.

Is there a default context we can use?
How should the serialization options be configured if we want backwards compatibility with the non-trimmed code?
Do we need to register every request/response object (used in controllers) manually?

In your example, I don't think LambdaJsonContext is pre-defined?

In the case of this example, it was clear we needed to register APIGatewayHttpApiV2ProxyRequest and APIGatewayHttpApiV2ProxyResponse.

@bhoradc bhoradc added needs-reproduction This issue needs reproduction. and removed needs-review labels Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. module/lambda-client-lib needs-reproduction This issue needs reproduction. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

4 participants