-
Notifications
You must be signed in to change notification settings - Fork 863
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
Amazon.Util.Internal.PlatformServices is not trimmable. MissingMethodException No parameterless constructor defined for type 'Amazon.Util.Internal.PlatformServices.NetworkReachability' #2531
Comments
@Beau-Gosse-dev Thanks for opening the issue. Just wanted to check if you referred the blog post Building serverless .NET applications on AWS Lambda using .NET 7 for the Native AOT support. As mentioned, currently, the Thanks, |
Hi @ashishdhingra , thanks for checking! Yup, I actually helped James review that blog before publishing. I'm opening this and maybe more issue in the future so that once we can get around to fixing trimming in the SDK it will be easier to identify problem areas. I also mentioned a work around in the |
### Why The published build of bmx right now will crash with the error `No parameterless constructor defined for type 'Amazon.Util.Internal.PlatformServices.NetworkReachability'` https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained When building bmx with aot, we reduce the deployment size by getting rid of unused parts of libraries. Theres a [problem ](aws/aws-sdk-net#2531) though with the aws sdk right now where it gets rid of some parts we actually need. I think it's to do with [this](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/incompatibilities#dynamic-assembly-loading-and-execution) ### How The github issue page mentioned a temporary solution to set the trim mode to partial for now. The build size would be 17.5MB compared to 13 so it seems like not a big issue?
@Beau-Gosse-dev Good afternoon. I was going through existing open issue and came across this one. The issue doesn't appear to be reproducible using the latest .NET 8 Native AOT blueprint and current latest version <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Generate Native AOT image during publishing to improve cold start time. -->
<PublishAot>true</PublishAot>
<!-- StripSymbols tells the compiler to strip debugging symbols from the final executable if we're on Linux and put them into their own file.
This will greatly reduce the final executable's size.-->
<StripSymbols>true</StripSymbols>
<!-- TrimMode partial will only trim assemblies marked as trimmable. To reduce package size make all assemblies trimmable and set TrimMode to full.
If there are trim warnings during build, you can hit errors at runtime.-->
<TrimMode>partial</TrimMode>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.11.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.3.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageReference Include="AWSSDK.SecretsManager" Version="3.7.400.45" />
</ItemGroup>
</Project Function.cs using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using System.Text.Json.Serialization;
using Amazon;
using Amazon.SecretsManager;
using Amazon.SecretsManager.Model;
namespace NativeAOTTrimming_Issue2531;
public class Function
{
/// <summary>
/// The main entry point for the Lambda function. The main function is called once during the Lambda init phase. It
/// initializes the .NET Lambda runtime client passing in the function handler to invoke for each Lambda event and
/// the JSON serializer to use for converting Lambda JSON format to the .NET types.
/// </summary>
private static async Task Main()
{
Func<string, ILambdaContext, string> handler = FunctionHandler;
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<LambdaFunctionJsonSerializerContext>())
.Build()
.RunAsync();
}
/// <summary>
/// A simple function that takes a string and does a ToUpper.
///
/// To use this handler to respond to an AWS event, reference the appropriate package from
/// https://github.com/aws/aws-lambda-dotnet#events
/// and change the string input parameter to the desired event type. When the event type
/// is changed, the handler type registered in the main method needs to be updated and the LambdaFunctionJsonSerializerContext
/// defined below will need the JsonSerializable updated. If the return type and event type are different then the
/// LambdaFunctionJsonSerializerContext must have two JsonSerializable attributes, one for each type.
///
// When using Native AOT extra testing with the deployed Lambda functions is required to ensure
// the libraries used in the Lambda function work correctly with Native AOT. If a runtime
// error occurs about missing types or methods the most likely solution will be to remove references to trim-unsafe
// code or configure trimming options. This sample defaults to partial TrimMode because currently the AWS
// SDK for .NET does not support trimming. This will result in a larger executable size, and still does not
// guarantee runtime trimming errors won't be hit.
/// </summary>
/// <param name="input">The event for the Lambda function handler to process.</param>
/// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param>
/// <returns></returns>
public static string FunctionHandler(string input, ILambdaContext context)
{
return GetSecret().Result;
}
static async Task<string> GetSecret()
{
string secretName = "MyTestSecret";
string region = "us-east-2";
IAmazonSecretsManager client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName(region));
GetSecretValueRequest request = new GetSecretValueRequest
{
SecretId = secretName,
VersionStage = "AWSCURRENT", // VersionStage defaults to AWSCURRENT if unspecified.
};
GetSecretValueResponse response;
try
{
response = await client.GetSecretValueAsync(request);
}
catch (Exception e)
{
// For a list of the exceptions thrown, see
// https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
throw e;
}
return response.SecretString;
// Your code goes here
}
}
/// <summary>
/// This class is used to register the input event and return type for the FunctionHandler method with the System.Text.Json source generator.
/// There must be a JsonSerializable attribute for each type used as the input and return type or a runtime error will occur
/// from the JSON serializer unable to find the serialization information for unknown types.
/// </summary>
[JsonSerializable(typeof(string))]
public partial class LambdaFunctionJsonSerializerContext : JsonSerializerContext
{
// By using this partial class derived from JsonSerializerContext, we can generate reflection free JSON Serializer code at compile time
// which can deserialize our class and properties. However, we must attribute this class to tell it what types to generate serialization code for.
// See https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-source-generation
} Please verify if the issue fixed at your end and confirm if this could be closed. Thanks, |
@ashishdhingra I'm closing this because I know this has been addressed and I know @Beau-Gosse-dev has build and deploy many Native AOT functions since we have addressed the issue. |
Comments on closed issues are hard for our team to see. |
Describe the bug
When publishing a .NET Lambda with native AOT that uses Secrets Manager, the below exception is thrown at runtime.
Expected Behavior
No runtime exception
Current Behavior
Reproduction Steps
Create a secret in Secrets Manager called 'MyTestSecret' in us-east-1.
Create a native AOT function with
dotnet new lambda.NativeAOT
Add the Amazon.SecretsManager NuGet package to the project.
Update the function code to be like below:
Possible Solution
Attribute the
NetworkReachability
class (along with potentially others) with theDynamicallyAccessedMembers
attribute.Similar to how we fix a similar issue here: aws/aws-lambda-dotnet#1382
Or remove the dynamic runtime loading of classes by updating the logic.
A work around is to use
<TrimMode>partial</TrimMode>
, however this increase the binary size from 5Mb to 11Mb.Additional Information/Context
Project file to reproduce:
AWS .NET SDK and/or Package version used
.NET 7 with native AOT
Targeted .NET Platform
.NET 7
Operating System and version
Built on Docker on Windows 10 Host, Deployed to AL2
The text was updated successfully, but these errors were encountered: