Pathoschild / FluentHttpClient

A modern async HTTP client for REST APIs. Its fluent interface lets you send an HTTP request and parse the response in one go.
MIT License
342 stars 52 forks source link

unity il2cpp build error #105

Closed muveso closed 2 years ago

muveso commented 2 years ago

Hi,

I want to use FluentHttpClient in Unity - Android. it works with mono builds but doesn't work with il2cpp. I would be very grateful if you could help me resolve this issue. because there is no unity rest client service that uses async infrastructure.

NotImplementedException: The method or operation is not implemented.
System.Runtime.Serialization.XsdDataContractExporter..ctor () (at <00000000000000000000000000000000>:0)

System.Net.Http.FormattingUtilities..cctor () (at <00000000000000000000000000000000>:0)

System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor () (at <00000000000000000000000000000000>:0)

System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters () (at <00000000000000000000000000000000>:0)

System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor () (at <00000000000000000000000000000000>:0)

Pathoschild.Http.Client.FluentClient..ctor (System.Uri baseUri, System.Net.Http.HttpClient baseClient, System.Boolean manageBaseClient) (at <00000000000000000000000000000000>:0)

System.Runtime.CompilerServices.AsyncTaskCache.CreateCacheableTask[TResult] (TResult result) (at <00000000000000000000000000000000>:0)

Test.Start () (at <00000000000000000000000000000000>:0)
Rethrow as TypeInitializationException: The type initializer for 'System.Net.Http.FormattingUtilities' threw an exception.

System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor () (at <00000000000000000000000000000000>:0)
System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters () (at <00000000000000000000000000000000>:0)
System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor () (at <00000000000000000000000000000000>:0)
Pathoschild.Http.Client.FluentClient..ctor (System.Uri baseUri, System.Net.Http.HttpClient baseClient, System.Boolean manageBaseClient) (at <00000000000000000000000000000000>:0)
System.Runtime.CompilerServices.AsyncTaskCache.CreateCacheableTask[TResult] (TResult result) (at <00000000000000000000000000000000>:0)
Test.Start () (at <00000000000000000000000000000000>:0)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <00000000000000000000000000000000>:0)
System.Threading.SendOrPostCallback.Invoke (System.Object state) (at <00000000000000000000000000000000>:0)
UnityEngine.UnhandledExceptionHandler+<>c.<RegisterUECatcher>b__0_0 (System.Object sender, System.UnhandledExceptionEventArgs e) (at <00000000000000000000000000000000>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <00000000000000000000000000000000>:0)
UnityEngine.UnitySynchronizationContext:Exec()
Pathoschild commented 2 years ago

Hi! Which .NET profile are you using in Unity?

muveso commented 2 years ago

@Pathoschild I am using .NET 4.x APIs.

Pathoschild commented 2 years ago

@muveso The error is happening in the lower-level .NET HttpClient due to part of .NET Framework being unimplemented. I don't use Unity myself, but this bit from Using .NET 4.x in Unity: IL2CPP Considerations might be relevant:

[...] Unity will attempt to strip out unused code during the IL2CPP export process. While this typically isn't an issue, with libraries that use Reflection, it can accidentally strip out properties or methods that will be called at run time that can't be determined at export time. To fix these issues, add a link.xml file to your project which contains a list of assemblies and namespaces to not run the stripping process against. For full details, please see Unity's docs on bytecode stripping.

And from the linked Unity docs:

Serialization AOT platforms might encounter issues with serialization and deserialization because of the use of reflection. If a type or method is only used via reflection as part of serialization or deserialization, the AOT compiler cannot detect that it needs to generate the code needs for the type or method.

You can try temporarily turning off code stripping to see if that fixes the error. If it does, you can add a link.xml which lists the specific types that are needed.