-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add support for Native AOT (Retarget library to .NET 8) #184
base: v4sdk-development
Are you sure you want to change the base?
Conversation
@@ -40,6 +41,13 @@ public DefaultMessageManagerFactory(IServiceProvider serviceProvider) | |||
/// <inheritdoc/> | |||
public IMessageManager CreateMessageManager(ISQSMessageCommunication sqsMessageCommunication, MessageManagerConfiguration configuration) | |||
{ | |||
return ActivatorUtilities.CreateInstance<DefaultMessageManager>(_serviceProvider, sqsMessageCommunication, configuration); | |||
var manager = new DefaultMessageManager( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ActivatorUtilities.CreateInstance
call here actually compiled with out trim warnings but failed at the runtime. Easy fix was to directly create the instance instead of relying on ActivatorUtilities.CreateInstance
.
@@ -15,20 +15,22 @@ | |||
<PackageReadmeFile>README.md</PackageReadmeFile> | |||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> | |||
<WarningsAsErrors>CA1727</WarningsAsErrors> | |||
<NoWarn>CS1591</NoWarn> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CS1591 is the code for no docs on public API surface. I had to add this because the code generated via the JSON serializer for the internal MessageJsonSerializerContext
doesn't have any docs. I tried a variety of ways to get the warning to ignore that generated code but nothing stuck.
Issue
#109
#144
Description of changes:
This PR enables
IsTrimmable
in the 3 packages and address all trim warnings. Note this is targeting the branch that will use V4 of the AWS SDK which has more Native AOT work done for that is required for this library.The main difference from user's point of view is to use Native AOT they must use the
AddMessageBus
overload that takes in aJsonSerializerContext
. That context must have the source generation setup for all of the POCOs used by the user that represent messages. If they do not use this overload the user will get a trim warning about the code requiring unreferenced code.I also the libraries to target .NET 8.0 to get the SDK's Native AOT support. Since .NET 6.0 is out of support and we haven't GA the library yet we don't need to start the GA lifecycle on unsupported targets.
The loading messaging config from
IConfiguration
via theLoadConfigurationFromSettings
method is not supported for trimming because that will always requiring loading types that are not known at compile time.The trickiest part for adding trimming support was the use of
MakeGenericType
in EnvelopeSerializer. In that class we are taking the user's Type for the message and turning that intoMessageEnvelope<User'sType>
. That was using the type of reflection via theMakeGenericType
andActivator.CreateInstance
that doesn't work in a trimming environment. That is because when working with theType
object you can't know at compile time what the type is. To handle that I created an envelope factory on theSubscriberMapping
that uses the generic parameters to create theMessageEnvelope<User'sType>
. Since the generic parameters are known at compile time we don't have to use theMakeGenericType
andActivator.CreateInstance
methods.Testing
Ran unit and integ tests and updated tests to handle new code paths when appropiate.
Build a console application with
PublishAot
set to true. The code handled publishing and subscribe and no trim warnings were produced and application ran successful.Code of console application
Json serializer context:
Message Handler:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.