Reputation: 379
I'm working with the windows 10 Windows.ApplicationModel.Chat API. I can't seem to figure out how to read the data from the DataStreamReference property in the ChatMessageAttachment object. Here's my code so far.
As you can see, it throws an exception with the message "Specified cast is not valid." The IBuffer interface makes no sense since all is has is the Length and Capacity property.
I'll appreciate if someone can give me some tips on how to read from that DataStreamReference property.
var store = await ChatMessageManager.RequestStoreAsync();
var conversations = await store.GetConversationReader().ReadBatchAsync();
foreach (var conversation in conversations)
{
var messages = (await conversation.GetMessageReader().ReadBatchAsync(int.MaxValue)).ToArray();
foreach (var message in messages)
{
try
{
var dataStream = await message.Attachments.First().DataStreamReference;
var stream = await dataStream.OpenReadAsync();
var buffer = new Ass(); // Implements IBuffer
var count = (uint)stream.Size;
// Throws exception - "Specified cast is not valid."
await stream.ReadAsync(buffer, count, Windows.Storage.Streams.InputStreamOptions.ReadAhead);
}
catch (Exception ex)
{
throw;
}
}
}
Upvotes: 0
Views: 140
Reputation: 2679
Try this:
using (var datastream = (await dataStream.OpenReadAsync()).AsStreamForRead())
{
StreamReader reader = new StreamReader(datastream);
string result = await reader.ReadToEndAsync();
}
Upvotes: 1