Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Resources\ResCategoryAttribute.cs">
<Link>Resources\ResCategoryAttribute.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs">
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Resources\ResDescriptionAttribute.cs">
<Link>Resources\ResDescriptionAttribute.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
}