每次我的代码调用AWS Lambda时,我都会遇到未处理的TaskCanceledException。该代码在Android设备上运行。 (它是用C#用Xamarin.Android编写的,并引用了AWSSDK.Core,AWSSDK.Lambda)。
代码:
MyAudioProcessingRunnable
Visual Studio在等待行中断,告诉我有一个未处理的public class SomeActivity: Activity
{
private AmazaonLambdaClient mAWSLambdaClient;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(...);
FindViewById(...).Click += ButtonClickAsync;
// System.Diagnostics.Trace redirects to Log.Debug with TAG="System.Diagnostics.Trace"
System.Diagnostics.Trace.Listeners.Add(new MyAndroidTraceListener("System.Diagnostics.Trace"));
System.Diagnostics.Trace.TraceInformation("Android trace listener installed");
// AWS logs to System.Diagnostics
AWSConfigs.LoggingConfig.LogTo = LoggingOptions.SystemDiagnostics;
AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Always;
}
protected override void OnStart()
{
base.OnStart();
var idToken = ...
var awsCredentials = new CognitoAWSCredentials("IdentityPoolID", AWSConfig.RegionEndpoint);
awsCredentials.AddLogin("accounts.google.com", idToken);
mAWSLambdaClient = new AmazonLambdaClient(awsCredentials, AWSConfig.RegionEndpoint);
}
protected override void OnStop()
{
base.OnStop();
mAWSLambdaClient.Dispose();
mAWSLambdaClient = null;
}
private async void ButtonClickAsync(object sender, System.EventArgs e)
{
await DoSomethingAsync();
}
private async Task DoSomethingAsync()
{
var lambdaRequest = ...
try
{
var lambdaInvokeTask = mAWSLambdaClient.InvokeAsync(lambdaRequest);
invokeResponse = await lambdaInvokeTask; <= VS breaks here after ~30 to 60 seconds
}
catch (TaskCanceledException e) // also tried catching Exception, no luck
{
Log.Error(TAG, "Lambda Task Canceled: {0}, {1}", e.Message, e.InnerException);
return;
}
}
}
。很奇怪,我确实处理了这个例外。
在未处理的异常之后,我检查Visual Studio中的设备日志。我按TAG =&#34; System.Diagnostics.Trace&#34;过滤而我发现的只有:
TaskCanceledException: a task was canceled
根据logging-with-the-aws-sdk-for-net,我应该在哪里获得AWS SDK日志?
更新
我已经弄清了问题1,为什么它会超时。这是由于base apk Information 0:
Android trace listener installed
将lambdaRequest
设置为PayloadStream
,在JSON将对象序列化为流后,其位置未重置为0。
我还没弄清楚为什么2,例外情况没有被try / catch处理,3,为什么AWS SDK没有按要求进行记录。
答案 0 :(得分:0)
我猜测function getBlocked(data) {
let openDb;
close() {
if (openDb) {
openDb.close().catch(err => {
console.log("Error closing db: ", err);
})
}
}
return MongoClient.connect(url).then(db => {
openDb = db;
const blQ = {blocked_user:data.tag_search_mail};
return db.collection("block_list").find(blQ,{"_id":0}).toArray();
}).then(results => {
let blocked = results.map(item => item.blocker);
close();
return blocked; // make this the resolved value of the promise
}).catch(err => {
close();
throw err; // rethrow to keep the promise rejected
})
}
// usage
getBlocked(data).then(blocked => {
// use blocked array here
}).catch(err => {
// handle error here
});
实例不是来自您的代码在catch语句中所期望的相同名称空间,或者它是从try-catch上方的行抛出,即{{ 1}}。如果您在try-catch块中移动该行并可能有更多行,会发生什么?
如果这没有帮助,请发布堆栈跟踪。