how to return the JSON of a javascript object in a WPF application-Collection of common programming errors

I have a WPF app with a WebBrowser control

I need to get the JSON string of a javascript object, something like this:

var json = this.WebBrowser.InvokeScript("eval", new object[] { "JSON.stringify(window.mainThing)" }); 

I am executing this code on WebBrowser’s LoadCompleted event

this throws an exception

System.Runtime.InteropServices.COMException was unhandled by user code

  HResult=-2147352319
  Message=Exception from HRESULT: 0x80020101
  Source=WindowsBase
  ErrorCode=-2147352319
  StackTrace:
       at MS.Internal.Interop.HRESULT.ThrowIfFailed(String message)
       at System.Windows.Controls.WebBrowser.InvokeScript(String scriptName, Object[] args)

Here’s the Javascript in the page:


   window.mainThing = { x : 1 };