从C#控制台应用程序使用Edge.js [英] Using Edge.js from a C# console application

查看:575
本文介绍了从C#控制台应用程序使用Edge.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些看起来很简单的事情,但是我无法使其正常工作.我已经通过nuget安装了Edge.js,我想从C#控制台应用程序内部执行一段Javascript.我的public static void main中的代码如下:

Task.Run(async () => {
    Console.WriteLine(
        await Edge.Func("return function() { return 'hello world'; }")(null).Result
    );
}).Wait();

我运行该应用程序,并且所有操作都挂起了.

我已经尝试过了:

Console.WriteLine(Edge.Func("return function() { return 'hello'; }")(null).Result);

相同的结果.这也是...

var task = Edge.Func("return function() { return 'hello'; }")(null);
await task;
Console.WriteLine(task.Result);

相同的结果.挂...

我也尝试了RunSynchronously()而不是等待,这给了我InvalidOperationException.

我在此处中遵循文档.. >

有什么想法吗?

解决方案

您必须使用回调函数来发送字符串.您不能只返回字符串.参见:

Edge.Func(@"
        return function (data, callback) {
            callback(null, 'hello world');
        }
    ");

使用Edge时必须遵循以下结构:

I'm trying to do something that seems very simple, yet I cannot get it to work. I've installed Edge.js via nuget and I want to execute a piece of Javascript from inside a C# console application. The code from my public static void main is below:

Task.Run(async () => {
    Console.WriteLine(
        await Edge.Func("return function() { return 'hello world'; }")(null).Result
    );
}).Wait();

I run the application and all it does is hangs.

I've tried this:

Console.WriteLine(Edge.Func("return function() { return 'hello'; }")(null).Result);

Same result. And this too...

var task = Edge.Func("return function() { return 'hello'; }")(null);
await task;
Console.WriteLine(task.Result);

Same result. Hanging...

I also tried RunSynchronously() rather than awaiting, which gave me an InvalidOperationException.

I followed the documentation here.

Any ideas?

解决方案

You have to use the callback to send the string. You can't just return the string. See:

Edge.Func(@"
        return function (data, callback) {
            callback(null, 'hello world');
        }
    ");

You have to follow this structure when using Edge:

这篇关于从C#控制台应用程序使用Edge.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆