I am building an ebook manager for the windows store and I have implemented the IUriToStreamResolver Interface on one of my classes. I am using that to open an epub. The code is as follows:
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if(uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
return GetContent(path).AsAsyncOperation();
}
private async Task<IInputStream> GetContent(string path)
{
path = path.TrimStart('/');
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(App.token);
var stream = await file.OpenAsync(FileAccessMode.Read);
var archive = new ZipArchive(stream.AsStream());
var entry = archive.Entries.Where(a => a.FullName == path);
var entryStream = entry.First().Open();
return entryStream.AsInputStream();
}
After returning from the GetContent()
method I get a cryptic System.InvalidCast exception. I enabled mixed debugging thinking I could get a little more information but it is not very helpful.
Here is the stack:
combase.dll!RoFailFastWithErrorContextInternal(long,unsigned long,struct _STOWED_EXCEPTION_INFORMATION_V1 * * const) Unknown combase.dll!_RoFailFastWithErrorContext@4() Unknown twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplication::ForwardLocalError(struct IRestrictedErrorInfo *) Unknown twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationFactory::ForwardLocalError(struct IRestrictedErrorInfo *) Unknown combase.dll!CallErrorForwarder(void *,int,struct IRestrictedErrorInfo *) Unknown combase.dll!_RoReportUnhandledError@4() Unknown mscorlib.ni.dll!63eb2d62() Unknown [Frames below may be incorrect and/or missing, no symbols loaded for mscorlib.ni.dll]
[Managed to Native Transition]
mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.RoReportUnhandledError(System.Runtime.InteropServices.WindowsRuntime.IRestrictedErrorInfo error) Unknown mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.ReportUnhandledError(System.Exception e) Unknown System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore() Unknown System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(object thisObj) Unknown mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.Invoke() Unknown [Native to Managed Transition]
Windows.UI.dll!Windows::UI::Core::CDispatcher::ProcessInvokeItem() Line 794 C++ Windows.UI.dll!Windows::UI::Core::CDispatcher::WaitAndProcessMessages(void * hEventWait) C++ Windows.UI.dll!Windows::UI::Core::CDispatcher::ProcessEvents(Windows::UI::Core::CoreProcessEventsOption options=CoreProcessEventsOption_ProcessUntilQuit) Line 390 C++ Windows.UI.Xaml.dll!DirectUI::FrameworkView::Run() Unknown twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationView::Run(void) Unknown twinapi.appcore.dll!Windows::Foundation::Collections::Internal::HashMap,struct Windows::Foundation::Collections::Internal::DefaultEqualityPredicate,struct Windows::Foundation::Collections::Internal::DefaultLifetimeTraits,struct Windows::ApplicationModel::Core::Details::SmugglableInterfaceLifetimeTraits,struct Windows::Foundation::Collections::Internal::HashMapOptions,0,1,0> >::Remove(unsigned int) Unknown SHCore.dll!Microsoft::WRL::RuntimeClass,class CScalingInfoBase,struct ICurrentWindowChangeListener,class Microsoft::WRL::FtmBase,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil,class Microsoft::WRL::Details::Nil>::`vector deleting destructor'(unsigned int) Unknown kernel32.dll!@BaseThreadInitThunk@12() Unknown ntdll.dll!__RtlUserThreadStart() Unknown ntdll.dll!__RtlUserThreadStart@8() Unknown
And here is the only local: $exceptionstack
[9 Frames, combase.dll!_RoOriginateLanguageException@12()]
[0] combase.dll!_RoOriginateLanguageException@12() void*
[1] mscorlib.ni.dll!63eb2c8a() void*
[2] mscorlib.ni.dll!63f4ffa2() void*
[3] mscorlib.ni.dll!63f4fd61() void*
[4] System.Runtime.WindowsRuntime.ni.dll!506ef9df() void*
[5] System.Runtime.WindowsRuntime.ni.dll!506ef965() void*
[6] mscorlib.ni.dll!63823156() void*
[7] System.Runtime.WindowsRuntime.ni.dll!506ef934() void*
[8] Windows.UI.ni.dll!50e1ff16() void*
Any help or direction is much appreciated!
I fixed it but I am not sure it is the optimal solution. If anyone would like to suggest improvements I am game.
In my GetContent()
method I made the following changes:
private async Task<IInputStream> GetContent(string path)
{
try
{
path = path.TrimStart('/');
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsyn(App.token);
var archive = new ZipArchive(await file.OpenStreamForReadAsync(),ZipArchiveMode.Read);
var entry = archive.GetEntry(path);
var contents = new byte[entry.Length];
var entryStream = entry.Open();
var tempFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("temp.xhtml",CreationCollisionOption.ReplaceExisting);
var decompressedStream = await tempFile.OpenStreamForWriteAsync();
await entryStream.CopyToAsync(decompressedStream, (int)entry.Length);
await decompressedStream.FlushAsync();
decompressedStream.Dispose();
var returnFile = await ApplicationData.Current.LocalFolder.GetFileAsync("temp.xhtml");
var returnStream = await returnFile.OpenSequentialReadAsync();
return returnStream;
}
catch (Exception e)
{
throw;
}
}