restsharp / RestSharp

Simple REST and HTTP API Client for .NET
https://restsharp.dev
Apache License 2.0
9.53k stars 2.34k forks source link

NativeAOT support? #2152

Open emrys90 opened 8 months ago

emrys90 commented 8 months ago

It would be nice to have full NativeAOT support for RestSharp. It currently logs trimming warnings.

/root/.nuget/packages/restsharp/110.2.0/lib/net7.0/RestSharp.dll : warning IL3053: Assembly 'RestSharp' produced AOT analysis warnings.
alexeyzimarev commented 3 months ago

The AOT support is blocked by reflections code in AddObject and AddObjectStatic. If you don't use those, it might be already working.

emrys90 commented 3 months ago

The AOT support is blocked by reflections code in AddObject and AddObjectStatic. If you don't use those, it might be already working.

Adding attributes to those methods can fix those warnings, and should be able to do so without changing any functionality. Here's a link to an article that explains how to support NativeAOT. https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/

vedantmgoyal9 commented 3 months ago

If you don't use those, it might be already working.

It still produces warnings. Is there any way we can make it AOT-compatible using the guide suggested by @emrys90?

alexeyzimarev commented 1 month ago

Ok, I tried now and the embedded XML serialiser creates all sort of pain. Basically, its Serialize function must be annotated as incompatible, which also requires that the interface method must be annotated as well. Which then makes all serialisers incompatible, and nothing works.

I only see one option: remove XML from formats supported by default, move all XML stuff to the XML package (existing one). Thoughts?

alexeyzimarev commented 1 month ago

Further research (I never used source generator provided by System.Text.Json before) shows that the only way to make it work with trimming is to use JsonSerializerContext created by the user. Therefore, there's no way to implement it with current set of serialisation abstractions as the context needs to be provided when calling the serialiser.

Which means that the scope of change is way beyond "just" adding some attributes as the whole serialisation story will be annotated and the only way it will work with trimming is to make calls without body and with responses that don't require deserialisation.

To me, it kind of defeats the purpose of using RestSharp at all.