How does ThrowTerminatingError work in C #?

I have the following cmdlet written in C #, it basically just throws an error:

[Cmdlet("Use", "Dummy")] public class UseDummyCmdlet :PSCmdlet { protected override void ProcessRecord() { var errorRecord = new ErrorRecord(new Exception("Something Happened"), "SomethingHappened", ErrorCategory.CloseError, null); ThrowTerminatingError(errorRecord); } } 

I assume (I could be wrong), this is the equivalent in PowerShell)

 Function Use-Dummy() { [CmdletBinding()] Param() process { $errorRecord = New-Object System.Management.Automation.ErrorRecord -ArgumentList (New-Object System.Exception), 'SomethingHappened', 'NotSpecified', $null $PSCmdlet.ThrowTerminatingError($errorRecord) } } 

The PowerShell version behaves as expected:

 Use-Dummy : Exception of type 'System.Exception' was thrown. At line:1 char:10 + use-dummy <<<< + CategoryInfo : NotSpecified: (:) [Use-Dummy], Exception + FullyQualifiedErrorId : SomethingHappened,Use-Dummy 

However, the C # version is crashing, with the following information:

 An exception of type 'System.Management.Automation.PipelineStoppedException' occurred in System.Management.Automation.dll but was not handled in user code Additional information: The pipeline has been stopped. 

What am I doing wrong?

+5
source share
1 answer

It can be confirmed that this is a problem of the environment, and damn strange.

Basically, if you follow the instructions here , then go on to debug any binary modules, if you call a ThrowTerminatingError , it crashes with a PipelineStoppedException .

Now I need some kind of fix / workaround.

Edit: a fix is ​​found, check the 'enable native code debugging' in the project properties.

+1
source

Source: https://habr.com/ru/post/1232970/


All Articles