discord-net / Discord.Net

An unofficial .Net wrapper for the Discord API (https://discord.com/)
https://discordnet.dev
MIT License
3.34k stars 737 forks source link

[Bug]: Native AOT deployment NET 8 #2694

Open LonerKO opened 1 year ago

LonerKO commented 1 year ago

Check The Docs

Verify Issue Source

Check your intents

Description

AOT deployment not works.

There are some bugs with JSON serialization and deserialization.

[Native-AOT] Using Json Serialize and Deserialize with Native AOT https://github.com/dotnet/runtimelab/issues/635

How to use source generation in System.Text.Json https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation

Please add support for native AOT deployment.

Version

3.10.0

Working Version

-

Logs

Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Discord.API.Rest.GetBotGatewayResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'url', line 1, position 7.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader, JsonObjectContract, JsonProperty, JsonProperty, String, Boolean&) + 0x191
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object) + 0x2ab
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader, Type, JsonContract, JsonProperty, JsonContainerContract, JsonProperty, Object) + 0xa4
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader, Type, Boolean) + 0x257
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader, Type) + 0xf3
   at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader) + 0x2f
   at Discord.API.DiscordRestApiClient.DeserializeJson[T](Stream) + 0x9a
   at Discord.API.DiscordRestApiClient.<SendAsync>d__73`1.MoveNext() + 0x17b
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x42
   at Discord.API.DiscordRestApiClient.<GetBotGatewayAsync>d__81.MoveNext() + 0x1d2
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x42
   at Discord.API.DiscordSocketApiClient.<ConnectInternalAsync>d__34.MoveNext() + 0x247
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at Discord.API.DiscordSocketApiClient.<ConnectInternalAsync>d__34.MoveNext() + 0x457
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x42
   at Discord.API.DiscordSocketApiClient.<ConnectAsync>d__33.MoveNext() + 0x1b0
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x42
   at Discord.WebSocket.DiscordSocketClient.<OnConnectingAsync>d__114.MoveNext() + 0x2fa
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x42
   at Discord.ConnectionManager.<ConnectAsync>d__31.MoveNext() + 0x5f0
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext() + 0x196

Sample

No response

Packages

Newtonsoft.Json - 13.0.3 Discord.Net - 3.10.0

ivanjx commented 1 year ago

i don't think it will ever be possible unless this library uses STJ. https://github.com/JamesNK/Newtonsoft.Json/issues/2732

edit: too bad https://github.com/discord-net/Discord.Net/pull/2493

Misha-133 commented 1 year ago

i don't think it will ever be possible unless this library uses STJ.

v4 will use STJ as the default serializer. will have to wait for now

ivanjx commented 1 year ago

@Misha-133 really? there was a PR about STJ but it was closed.

Misha-133 commented 1 year ago

@Misha-133 really? there was a PR about STJ but it was closed.

Yeah, but it was targeting v3. V4 will have reworked REST implementation with support for STJ.