Jer*_*son 5 c# freeze amazon-s3 amazon-web-services async-await
注意:回答我自己的问题以帮助将来的其他人。
我按照官方文档从 S3 存储桶获取文本文件,但它挂起:
static async Task ReadObjectDataAsync()
{
string responseBody = "";
try
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = keyName
};
//THIS NEXT LINE HANGS!!!!
using (GetObjectResponse response = await client.GetObjectAsync(request))
using (Stream responseStream = response.ResponseStream)
using (StreamReader reader = new StreamReader(responseStream))
{
string title = response.Metadata["x-amz-meta-title"];
Run Code Online (Sandbox Code Playgroud)
我该如何让它发挥作用?
更多解决方案请参见https://github.com/aws/aws-sdk-net/issues/152
对我来说,问题是我在 WinForm 应用程序中运行这个 AWS 示例。
默认情况下,Winform 应用程序的Main()方法使用 Single Threaded Apartment attribute 进行修饰 [STAThread]。这会导致异步失败。
删除该[STAThread]属性或创建另一个Main()没有该属性的方法。