diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 950d7f83b1..ee1c582c4b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -852,6 +852,9 @@ Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs + + Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs + Resources\ResCategoryAttribute.cs diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index 104b9261e7..746360b670 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -1016,6 +1016,9 @@ Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs + + Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs + Resources\ResDescriptionAttribute.cs diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs index d927155dbb..eb2d434b2c 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs @@ -133,13 +133,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto) // - If the payload exceeds 2,047 bytes but remains within sensible limits, we still send it, but note that // some servers may silently drop or reject such packets — behavior we may use for future probing or diagnostics. // - If payload exceeds 10KB even after dropping fields , we send an empty payload. - var options = new JsonSerializerOptions - { - PropertyNamingPolicy = null, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - WriteIndented = false - }; - byte[] payload = JsonSerializer.SerializeToUtf8Bytes(dto, options); + byte[] payload = JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto); // We try to send the payload if it is within the limits. // Otherwise we drop some fields to reduce the size of the payload and try one last time @@ -157,7 +151,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto) dto.OS.Details = null; // drop OS.Details } - payload = JsonSerializer.SerializeToUtf8Bytes(dto, options); + payload = JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto); if (payload.Length <= JsonPayloadMaxBytes) { return payload; @@ -166,7 +160,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto) dto.OS = null; // drop OS entirely // Last attempt to send minimal payload driver + version only // As per the comment in AdjustJsonPayloadSize, we know driver + version cannot be larger than the max - return JsonSerializer.SerializeToUtf8Bytes(dto, options); + return JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto); } internal static UserAgentInfoDto BuildDto() diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs new file mode 100644 index 0000000000..25af2cd2d6 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; + +#nullable enable + +namespace Microsoft.Data.SqlClient.UserAgent; + +[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.Unspecified)] +[JsonSerializable(typeof(UserAgentInfoDto), GenerationMode = JsonSourceGenerationMode.Serialization)] +internal sealed partial class UserAgentInfoDtoSerializerContext : JsonSerializerContext +{ +}