[英]“Unknown Runtime Error” when using Windows Runtime Component with Javascript UWP App
I'm trying to use a Windows Runtime Component to provide interoperability between my Javascript UWP app and C# logic that I've written. 我正在尝试使用Windows运行时组件在Javascript UWP应用程序和我编写的C#逻辑之间提供互操作性。 If I set the minimum version to Fall Creator's Update (build 16299, needed to use .NET Standard 2.0 libraries), I get the following error when trying to call a simple method:
如果我将最低版本设置为Fall Creator's Update(内部版本16299,需要使用.NET Standard 2.0库),则在尝试调用一个简单方法时出现以下错误:
Unhandled exception at line 3, column 1 in ms-appx://ed2ecf36-be42-4c35-af69-93ec1f21c283/js/main.js
0x80131040 - JavaScript runtime error: Unknown runtime error
If I run this code using Creator's Update (15063) as the minimum, then the code runs fine. 如果我至少使用创建者更新(15063)运行此代码,则该代码可以正常运行。
I've created a Github repo containing a sample solution that generates the error for me when running locally. 我创建了一个包含示例解决方案的Github存储库 ,该示例解决方案在本地运行时会为我生成错误。
Here's what main.js looks like. 这是main.js的样子。 The error occurs when trying to run the getExample function:
尝试运行getExample函数时发生错误:
// Your code here!
var test = new RuntimeComponent1.Class1;
test.getExample().then(result => {
console.log(result);
});
This is what Class1.cs looks like: 这是Class1.cs的样子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
namespace RuntimeComponent1
{
public sealed class Class1
{
public IAsyncOperation<string> GetExample()
{
return AsyncInfo.Run(token => Task.Run(getExample));
}
private async Task<string> getExample()
{
return "It's working";
}
}
}
I can't think of a much simpler test case than that - I have no NuGet packages installed or anything like that. 我想不到一个比这简单得多的测试用例-我没有安装NuGet软件包或类似的东西。 I have no idea what might be causing this.
我不知道是什么原因造成的。 Anyone else have ideas?
其他人有想法吗?
There is nothing actually async about this function, even as a simplified example 实际上,此功能没有任何异步,即使是一个简化的示例
private async Task<string> getExample()
{
return "It's working";
}
Also if the said function is already return a Task
then there is no need to wrap it in Task.Run
here 另外,如果所说的函数已经返回一个
Task
则无需将其包装在Task.Run
。
return AsyncInfo.Run(token => Task.Run(getExample));
Refactor the code to follow advised syntax 重构代码以遵循建议的语法
public sealed class Class1 {
public IAsyncOperation<string> GetExampleAsync() {
return AsyncInfo.Run(token => getExampleCore());
}
private Task<string> getExampleCore() {
return Task.FromResult("It's working");
}
}
Since there is nothing to be awaited, use Task.FromResult
to return the Task<string>
from the private getExampleCore()
function. 由于没有什么可等待的,所以使用
Task.FromResult
从私有getExampleCore()
函数返回Task<string>
。
Do note also that because the original functions were returning unstarted tasks, that this causes an InvalidOperationException
to be thrown by AsyncInfo.Run<TResult>(Func<CancellationToken, Task<TResult>>)
Method 还要注意,由于原始函数正在返回未启动的任务,因此这将导致
AsyncInfo.Run<TResult>(Func<CancellationToken, Task<TResult>>)
方法引发InvalidOperationException
You can also consider taking advantage of the AsAsyncOperation<TResult>
extension method, given the simple definition of the called function. 给定调用函数的简单定义,您还可以考虑利用
AsAsyncOperation<TResult>
扩展方法。
public IAsyncOperation<string> GetExampleAsync() {
return getExampleCore().AsAsyncOperation();
}
And invoked in JavaScript 并在JavaScript中调用
var test = new RuntimeComponent1.Class1;
var result = test.getExampleAsync().then(
function(stringResult) {
console.log(stringResult);
});
This is not proper async method: 这是不正确的异步方法:
private async Task<string> getExample()
{
return "It's working";
}
The reason for this is that it was supposed to return Task<string>
, not just string
. 这样做的原因是应该返回
Task<string>
,而不仅仅是返回string
。
So, you should change it to: 因此,您应该将其更改为:
private async Task<string> getExample()
{
return Task.FromResult("It's working");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.