Javascript.NodeJS icon indicating copy to clipboard operation
Javascript.NodeJS copied to clipboard

How to call Node JS script and possibly with parameter from WinForms App?

Open zydjohnHotmail opened this issue 1 year ago • 0 comments

Hello: I recently need to use node npm package (ccxt) and return Json format string to C#.

The following is my situation: My OS: Windows 10 (version 21H2) Node.js: 18.10.0 (x64) I have installed the following npm packages: D:\nodejs\CCXT_Address>npm list ccxt CCXT_Address@ D:\nodejs\CCXT_Address `-- [email protected]

The following is my Node JS script, it works: const ccxt = require('ccxt');

var json_data = async () => { let aax = new ccxt.aax({ apiKey: 'My_API_Key', secret: 'My_Secret_Key', }) try { const deposits = await aax.fetchDepositAddress('BTC'); console.log(deposits); } catch (e) { console.log(e.constructor.name, e.message) } }; json_data(); I can run my Node.js script (index.js) without any issue: D:\nodejs\CCXT_Address>node AAX_BTC_Address0.js (node:35092) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time (Use node --trace-warnings ... to show where the warning was created) { info: { address: '1PG9DqK7QXtXDJZQducsbCy...', currency: 'BTC', tag: 'Grayscale', network: 'BTC' }, currency: '', address: '1PG9DqK7QXtXDJZQducsbCy...', tag: 'Grayscale', network: 'BTC' }

I created one C# WinForms App target .NET 6.0 I added the nuget page: Install-Package Jering.Javascript.NodeJS -Version 6.3.1 I rewrite my Node JS code: D:\nodejs\CCXT_Address>type AAX_BTC_Address.js const ccxt = require('ccxt');

module.exports = (callback) => { var json_data = async () => { let aax = new ccxt.aax({ apiKey: 'My_API_Key', secret: 'My_Secret_Key', }) try { const deposits = await aax.fetchDepositAddress('BTC'); console.log(deposits); callback(null, deposits); } catch (e) { callback(null, '{}'); console.log(e.constructor.name, e.message) } } }

D:\nodejs\CCXT_Address>

The following is my C# code in Form1.cs: using Jering.Javascript.NodeJS; using System.Diagnostics; #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.

namespace CallNodeJSWithParameterForm { public partial class Form1 : Form { public const string AAC_BTC_JS = @"D:\nodejs\CCXT_Address\AAX_BTC_Address.js";

    public Form1()
    {
        InitializeComponent();
    }

    private async void BTNCallNodeJS_Click(object sender, EventArgs e)
    {
        StaticNodeJSService.Configure<OutOfProcessNodeJSServiceOptions>
            (options => options.TimeoutMS = 30000);
        string json_data =
            await StaticNodeJSService.InvokeFromFileAsync<string>(AAC_BTC_JS);
        Debug.Print("");
    }
}

} I can compile my C# code, but when I run my code, it simply times out. Inner Exception 1: InvocationException: The Node invocation timed out after 30000ms. You can change the timeout duration by setting the TimeoutMS property on OutOfProcessNodeJSServiceOptions. Do ensure that your NodeJS function always invokes the callback (or throws an exception synchronously), even if it encounters an error.

When I run my bare Node JS code, I usually wait less than 5 seconds, then I can see the result. But after I modified my Node JS as above, the code times out. Please let me know if I made any mistake in my Node JS code. By the way, I want to be able to pass parameter to Node JS code, so I can show different result, like: const deposits = await aax.fetchDepositAddress('BTC'); => const deposits = await aax.fetchDepositAddress('ETH'); Please advise,

zydjohnHotmail avatar Oct 12 '22 14:10 zydjohnHotmail