[CSHARP-3755] ObjectId serialization to JSON fails with PlatformNotSupportedException (Web Assembly) Created: 21/Jul/21  Updated: 27/Oct/23  Resolved: 13/Aug/21

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.13.0
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Ivari Horm Assignee: Dmitry Lukyanov (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 using Visual Studio 2019, running application in the IIS Express instance for debugging.


Issue Links:
Related
related to CSHARP-4551 Avoid using PID when generating Objec... Closed

 Description   

When I try to serialize an object that has ObjectId defined as a field, I get the PlatformNotSupportedException as the serializer tries to execute System.Diagnostics.Process which seems to be unsupported in .Net 5:

 Unhandled exception rendering component: The type initializer for 'MongoDB.Bson.ObjectId' threw an exception.
System.TypeInitializationException: The type initializer for 'MongoDB.Bson.ObjectId' threw an exception.
 ---> System.PlatformNotSupportedException: System.Diagnostics.Process is not supported on this platform.
   at System.Diagnostics.Process.GetCurrentProcess()
   at MongoDB.Bson.ObjectId.GetCurrentProcessId()
   at MongoDB.Bson.ObjectId.GetPid()
   at MongoDB.Bson.ObjectId.CalculateRandomValue()
   at MongoDB.Bson.ObjectId..cctor()
   --- End of inner exception stack trace ---
   at System.Text.Json.JsonPropertyInfo`1[[System.Int32, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1[[MongoDB.Bson.ObjectId, MongoDB.Bson, Version=2.13.0.0, Culture=neutral, PublicKeyToken=null]].OnTryWrite(Utf8JsonWriter writer, ObjectId value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1[[MongoDB.Bson.ObjectId, MongoDB.Bson, Version=2.13.0.0, Culture=neutral, PublicKeyToken=null]].TryWrite(Utf8JsonWriter writer, ObjectId& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonPropertyInfo`1[[MongoDB.Bson.ObjectId, MongoDB.Bson, Version=2.13.0.0, Culture=neutral, PublicKeyToken=null]].GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1[[CPM.Auth.Models.User, CPM.Auth, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnTryWrite(Utf8JsonWriter writer, User value, JsonSerializerOptions options, WriteStack& state)



 Comments   
Comment by Backlog - Core Eng Program Management Team [ 13/Aug/21 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by Dmitry Lukyanov (Inactive) [ 27/Jul/21 ]

Hello ivari@risk.ee , I looked deeper at this platform and found that there is much more that it lacks in functionality besides System.Diagnostics.Process, which means that currently the C# driver does not support the Web Assembly platform. One other example: WASM does not support the Task Parallel Library (TPL), upon which the C# driver heavily relies.

At this time we do not have plans to support the Web Assembly platform.

Comment by Ivari Horm [ 27/Jul/21 ]

Sorry for the delay, summer time causes hectic schedule.

I think there is on crucial bit of information lacking - the project is using net5.0 as Web Assembly (Blazor Wasm). Sorry about that, I forgot to include this in my original report.

However, Blazor Wasm code is executed inside a browser, the System.Process API is simply not available in a browser environment. I tried to dig around the Microsoft documentation, but could not find anything that explicitly states that, though it seems more than reasonable. There is an article about the unsupported API-s in a browser environment: https://devblogs.microsoft.com/dotnet/the-future-of-net-standard/#dealing-with-apis-that-are-unsupported-in-blazor-webassembly

 

Comment by Dmitry Lukyanov (Inactive) [ 26/Jul/21 ]

Hey ivari@risk.ee , as it was mentioned above, it looks like the issue is not related to .net5 itself. Can you please provide a small console application with self-contained reproduction?

Comment by Robert Stam [ 23/Jul/21 ]

The following also works in a console application using .NET 5:

var objectId = ObjectId.GenerateNewId();
var json = System.Text.Json.JsonSerializer.Serialize(objectId);

but System.Text.Json doesn't really understand our data types and serializes it differently than we would.

We serialize an ObjectId to JSON as:

 "{ \"$oid\" : \"60faf4e2cb3c7d3c4592252f\" }"

But System.Text.Json serializes an ObjectId to JSON as:

"{\"Timestamp\":1627059258,\"Machine\":15149218,\"Pid\":1815,\"Increment\":15716805,\"CreationTime\":\"2021-07-23T16:54:18Z\"}"

 

 

Comment by Robert Stam [ 23/Jul/21 ]

The following test works in a console application using .NET 5:

var objectId = ObjectId.GenerateNewId();
var json = objectId.ToJson();

So the problem is not a general .NET 5 problem. It must have something to do with the IIS environment.

Comment by Robert Stam [ 23/Jul/21 ]

I missed that you had provided that information in the Environment field: "Windows 10 using Visual Studio 2019, running application in the IIS Express instance for debugging."

 

Comment by Robert Stam [ 23/Jul/21 ]

Thank you for reporting this issue. It's somewhat surprising since the documentation for GetCurrentProcess says it is support by .NET 5.

Can you let us know what platform you were running on when you got this exception (Windows, Linux, OS X?).

Generated at Wed Feb 07 21:46:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.