jpsider / RestPS

Simple ReST Framework for Powershell
MIT License
114 stars 31 forks source link

Exception calling "Write" with "3" arguments #63

Closed infiniterecursion closed 2 years ago

infiniterecursion commented 2 years ago

I'm getting the following error when processing webhooks from only certain applications. Not sure what I need to look at to troubleshoot. The incoming data is processed fine, so I think this error may be when attempting to send a response. Could this be a configuration issue with the sending application (Team Foundation Server 2017)?

Exception calling "Write" with "3" argument(s): "The I/O operation has been aborted because of either a thread exit or an application request"

At C:\Program Files\WindowsPowerShell\Modules\RestPS\7.0.44\RestPS.psm1:340 char:5
+     $script:Response.OutputStream.Write($responseByteArray, 0, $respo ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : HttpListenerException
jpsider commented 2 years ago

Can you validate the data on the outgoing response? If I had to guess one of the values is null.

infiniterecursion commented 2 years ago

What would be the best way to go about that? When I send the same request data from TFS using Postman, I do not get the error, only when TFS directly sends the webhook. I'm not generating any data to send back to TFS.

jpsider commented 2 years ago

Best way would be to edit the module in the box and log the variables out to see if they are null.

infiniterecursion commented 2 years ago

I've tried modifying the RestPS.psm1 file to add either Write-Host output or using the provided Write-Log function but can't seem to get it to output anything. Do I need to reload the module or something? If I copy what was sent from the TFS webhook history menu and paste it into Postman I can send the same data without error. Could it be related to the headers?

infiniterecursion commented 2 years ago

Ok, I think I found my issue. It seems I was outputting some variable values for testing and the main module seems to be sending that as the response body to the initiating service. Right now I'm just using a return 0 out of my script, but should there be something else I need to return? The originating service is sending a webhook to the listener so I don't really need to transmit any information back to the originator.

jpsider commented 2 years ago

Sorry, I'm not sure I fully understand what's going on.

infiniterecursion commented 2 years ago

I think my issue was that I was using a return statement at the end of my processing script but did not realize that Powershell by default will return every variable that you output in a script file when you call a return statement. So the main RestPS wrapper script was expecting me to return one object back to it but I was in fact returning more due to some variables I was trying to output for testing. I think everything is okay now as I've gotten the error to go away by removing the variables I was outputting in my script, but I now have a question about what, if anything, I should be returning back to RestPS as a best practice.

jpsider commented 2 years ago

RestPS - you only need to send back the data you want to respond with from the web server, nothing more.