Skip to content

Commit c8f8a27

Browse files
Copilotstephentoub
andcommitted
Use #if NET to compile out error data population on .NET Framework
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 3209414 commit c8f8a27

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/ModelContextProtocol.Core/McpSessionHandler.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -454,25 +454,20 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
454454
{
455455
LogSendingRequestFailed(EndpointName, request.Method, error.Error.Message, error.Error.Code);
456456
var exception = new McpProtocolException($"Request failed (remote): {error.Error.Message}", (McpErrorCode)error.Error.Code);
457-
457+
458+
#if NET
458459
// Populate exception.Data with the error data if present.
459460
// When deserializing JSON, Data will be a JsonElement.
461+
// Note: This is not supported on .NET Framework because Exception.Data uses ListDictionaryInternal
462+
// which requires values to be marked with [Serializable], and JsonElement is not serializable.
460463
if (error.Error.Data is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Object)
461464
{
462465
foreach (var property in jsonElement.EnumerateObject())
463466
{
464-
try
465-
{
466-
exception.Data[property.Name] = property.Value;
467-
}
468-
catch (ArgumentException)
469-
{
470-
// On .NET Framework, Exception.Data uses ListDictionaryInternal which requires
471-
// values to be serializable. JsonElement is not marked as [Serializable], so
472-
// setting it throws ArgumentException. We silently skip such entries.
473-
}
467+
exception.Data[property.Name] = property.Value;
474468
}
475469
}
470+
#endif
476471

477472
throw exception;
478473
}

0 commit comments

Comments
 (0)