Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PSObject with .NET 8 plus Microsoft.PowerShell.SDK 7.4+ is not able to be serialized #3028

Open
vidai-msft opened this issue Mar 6, 2025 · 0 comments

Comments

@vidai-msft
Copy link

The following code works in .NET 6 + Microsoft.PowerShell.SDK 7.2 but not with .NET 8 + Microsoft.PowerShell.SDK 7.4.

var psobj = new PSObject();
psobj.Properties.Add(new PSNoteProperty("Name", "Vincent"));
JsonConvert.SerializeObject(psobj);

It will throw the following exception.
Unhandled exception. Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Value' with type 'System.Management.Automation.PSMethod[System.Management.Automation.MethodGroup1[System.Func`1[System.String]]]'. Path 'ToString'

The cause is this line https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs#L1240.

In PowerShell SDK 7.2, the PSObject class is marked as [Serializable] so that the matched contract is SerializableContract. However, in PowerShell SDK 7.4, this attribute has been removed because it is obsolete in .NET 8. So PSObject uses DynamicContract.

Can we get it fixed? Otherwise, PSObject is not able to work with Newtonsoft.Json.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant