异步任务引发NotImplementedException

时间:2015-05-07 12:30:10

标签: c# windows-phone-8.1 async-await .net-4.5

我继续努力使用Windows Phone编程。 :)

调用方法时:

public static async Task<string> ConvertToBase64(this BitmapImage bitmapImage)
{
    RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
    var streamWithContent = await rasr.OpenReadAsync();
    byte[] buffer = new byte[streamWithContent.Size];
    var result = await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
    using (MemoryStream ms = new MemoryStream(result.ToArray()))
    {
        return Convert.ToBase64String(ms.ToArray());
    }
}

public async void SetPhotoString(BitmapImage bi)
{
    photoBase64 = await bi.ConvertToBase64();
}
来自按钮回调的课程Offer中的

public async void Post()
{
    var newOffer = new Offer()
    {
        BookAuthor = Author,
        BookTitle = Title,
        CurrencyWorth = Price,
        Description = Description,
        StartedAt = DateTime.UtcNow,
        Status = OfferStatus.Added
    };
    newOffer.SetPhotoString(Photo);//this line throws an excp.
    var result = await offerService.AddOffer(newOffer);
    if (result != null)
        ClearFormula();
}

我收到了“NotImplementedException”。我想我的异步调用有问题,但我不知道是什么。

异常详情: message: The method or operation is not implemented.

    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SecondHandBookshop.Shared.Helpers.BitmapImageExtensions.<ConvertToBase64>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SecondHandBookshop.Shared.Models.Offer.<SetPhotoString>d__0.MoveNext()

更多信息: Photo是我的AddNewOfferViewModel的BitmapImage属性,它充当Image控件的源。它是从虚拟设备上的摄像头捕获的:

public async void TakeAPhoto()
    {
        ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("TestPhoto.jpg",
            CreationCollisionOption.GenerateUniqueName);
        await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, file);
        Photo = new BitmapImage(new Uri(file.Path));
        await MediaCapture.StopPreviewAsync();
        NotifyOfPropertyChange(() => Photo);
        ShowCaptureFrame = false;
        NotifyOfPropertyChange(() => ShowCaptureFrame);
    }

当我用以下内容替换给出和异常的行时:

newOffer.PhotoBase64 = await Photo.ConvertToBase64();

我得到一个例外:

    Additional information: Value does not fall within the expected range.

       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SecondHandBookshop.Shared.Helpers.BitmapImageExtensions.<ConvertToBase64>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SecondHandBookshop.WindowsPhone.ViewModels.AddOfferViewModel.<Post>d__1.MoveNext()

2 个答案:

答案 0 :(得分:0)

为什么您的代码ConvertToBase64还没有被等待?这是完全错误的,因为async void构造。

尝试将代码更改为以下内容:

newOffer.photoBase64 = await SetPhotoString(Photo);//this line throws an excp.

public async Task<string> SetPhotoString(BitmapImage bi)
{
    return await bi.ConvertToBase64();
}

此外,ConvertToBase64方法的哪一行会引发异常?
连续操作会发生异常吗?
如果不是,那么您用于async代码的某些类没有正确实现GetAwaiter方法,这会导致异常。
如果是,那么问题出在Photo属性中,您没有向我们展示。可能是您的代码中有一些未实现的方法。

答案 1 :(得分:0)

尝试在ConvertToBase64扩展方法中返回一个任务。

curl 'http://localhost:8983/solr/update/json?commit=true' --data-binary [URL] -H 'Content-type:application/json'