当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Async task raises NotImplementedException

问题描述:

I continue to struggle with Windows Phone programming. :)

When invoking the method:

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());

}

}

from

public async void SetPhotoString(BitmapImage bi)

{

photoBase64 = await bi.ConvertToBase64();

}

in class Offer from the button callback:

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();

}

I get the "NotImplementedException" raised. I guess I am doing something wrong with that async calls, but I have no idea what.

Exception details:

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()

Further info:

Photo is a BitmapImage property of my AddNewOfferViewModel, that acts as a source for the Image control. It is captured from the camera on the wirtual device:

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);

}

when I replace the line that gives and exception with:

newOffer.PhotoBase64 = await Photo.ConvertToBase64();

I get an exception:

 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()

网友答案:

Why your code for ConvertToBase64 isn't being awaited? This is completely wrong, as the async void construction.

Try to change your code to something like this:

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

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

Also, what line of the ConvertToBase64 method does throw the exception?
Does exception occur with continious operation?
If no, then some of the classes you are using for the async code didn't implement properly the GetAwaiter method, which leads you to the exception.
If yes, than the problem is in Photo property you didn't show us. May be there is some not implemented method in your code.

网友答案:

Try return a Task in your ConvertToBase64 extension method.

using (MemoryStream ms = new MemoryStream(result.ToArray()))
{
    return Task.FromResult(Convert.ToBase64String(ms.ToArray()));
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: