Deserialized class conversion error

I’m running a powershell script from .net that attempts to use remote WMI

$SessionOption = New-CimSessionOption -Culture $Script:InvariantCulture -UICulture $Script:InvariantCulture -Protocol DCOM

$sessionParameters = @{

ComputerName = $ComputerAddress

SessionOption = $SessionOption

Credential = $credentials

}

$session = New-CimSession @sessionParameters -ErrorAction SilentlyContinue

The script is running fine when running it from visual studio, however, when attempting to run a published executable of the .net project I’m getting the following error from the script:

Cannot bind parameter ‘SessionOption’. Cannot convert value “Microsoft.Management.Infrastructure.Options.DComSessionOptions” to type “Microsoft.Management.Infrastructure.Options.CimSessionOptions”. Error: "Cannot convert the “Microsoft.Management.Infrastructure.Options.DComSessionOptions” value of type “Deserialized.Microsoft.Management.Infrastructure.Options.DComSessionOptions” to type “Microsoft.Management.Infrastructure.Options.CimSessionOptions”

From what I can see DComSessionOptions inherits from CimSessionOptions (and indeed New-CimSessionOption returns DComSessionOptions in both versions for -Protocol DCOM). It seems it cannot cast it to CimSessionOptions because it got deserialized in the published version. However, I do not understand why it got deserialized in the published version and not in the regular release. Is there anyway to avoid it?

In both cases I’m using .net 5.0 and powershell 7.1.3