我在.Net应用程序中使用DotNetBrowser作为一种无头Javascript引擎。DotNetBrowser似乎不支持Promises,或者我没有正确调用它们。
在JS中:
window.fooAsyncMethod = () => {
return new Promise((resolve, reject) => {
resolve('async value to resolve');
});
}
在C#中:
var result = await browser.MainFrame.ExecuteJavascript("await window.fooAsyncMethod()")
在这种情况下,结果始终为null。我也尝试过async/await,并且总是收到null:
window.fooAsyncMethod = async () => {
return await someOtherAsyncMethod()
}
DotNetBrowser是否支持运行异步javascript?如果是这样的话,我如何实现等待异步操作的结果并在C#中解析该值?
查看JS-.NET桥接示例,它似乎确实支持promise,但它们的使用有点复杂。
如果你在C#端实现了一种promise对象:
public static class JsObjectExtensions
{
#region Methods
public static JsPromise AsPromise(this IJsObject jsObject) => JsPromise.AsPromise(jsObject);
#endregion
}
public sealed class JsPromise
{
private readonly IJsObject jsObject;
#region Constructors
private JsPromise(IJsObject jsObject)
{
this.jsObject = jsObject;
}
#endregion
public static JsPromise AsPromise(IJsObject jsObject) => !IsPromise(jsObject) ? null : new JsPromise(jsObject);
public JsPromise Catch(Func<object, object> onRejected)
{
IJsObject newPromise = jsObject.Invoke("catch", onRejected) as IJsObject;
return new JsPromise(newPromise);
}
public Task<Result> ResolveAsync()
{
TaskCompletionSource<Result> promiseTcs = new TaskCompletionSource<Result>();
Then(obj => { promiseTcs.TrySetResult(Fulfilled(obj)); },
obj => { promiseTcs.TrySetResult(Rejected(obj)); });
promiseTcs.Task.ConfigureAwait(false);
return promiseTcs.Task;
}
public void Then(Action<object> onFulfilled, Action<object> onRejected = null)
{
jsObject.Invoke("then", onFulfilled, onRejected);
}
public JsPromise Then(Func<object, object> onFulfilled, Func<object, object> onRejected = null)
{
IJsObject newPromise = jsObject.Invoke("then", onFulfilled, onRejected) as IJsObject;
return new JsPromise(newPromise);
}
private Result Fulfilled(object o) => new Result(ResultState.Fulfilled, o);
private static bool IsPromise(IJsObject jsObject)
{
if (jsObject == null || jsObject.IsDisposed)
{
return false;
}
IJsObject promisePrototype = jsObject.Frame.ExecuteJavaScript<IJsObject>("Promise.prototype").Result;
return promisePrototype.Invoke<bool>("isPrototypeOf", jsObject);
}
private Result Rejected(object o) => new Result(ResultState.Rejected, o);
public enum ResultState
{
Fulfilled,
Rejected
}
public class Result
{
public object Data { get; }
public ResultState State { get; }
internal Result(ResultState state, object data)
{
State = state;
Data = data;
}
}
}
然后你就可以这样称呼承诺:
IJsObject window = browser.MainFrame.ExecuteJavaScript<IJsObject>("window").Result;
//Prepare promise handlers
Action<object> promiseResolvedHandler = o => Console.WriteLine("Success: " + o);
Action<object> promiseRejectedHandler = o => Console.Error.WriteLine("Error: " + o);
//Create a promise that is fulfilled
Console.WriteLine("Create a promise that is fulfilled...");
IJsObject promise1 = window.Invoke<IJsObject>("CreatePromise", true);
//Append fulfillment and rejection handlers to the promise
promise1.Invoke("then", promiseResolvedHandler, promiseRejectedHandler);
//Create a promise that is rejected
Console.WriteLine("Create a promise that is rejected...");
IJsObject promise2 = window.Invoke<IJsObject>("CreatePromise", false);
//Append fulfillment and rejection handlers to the promise
promise2.Invoke("then", promiseResolvedHandler, promiseRejectedHandler);
CreatePromiseAsync(window).Wait();
请注意,以上所有代码都直接取自他们的Promises示例。