System.InvalidCast异常Windows运行时



我正在为windows商店构建一个电子书管理器,我已经在我的一个类上实现了IUriToStreamResolver接口。我正在用它来打开一个epub。代码如下:

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

GetContent()方法返回后,我得到一个神秘的System.InvalidCast异常。我启用了混合调试,认为我可以获得更多的信息,但这并没有太大帮助。

这是堆栈:

combase.dll!RoFailFastWithErrorContextInternal(long,unsigned long,struct_STOWED_EXCEPTION_INFORMATION_V1**const)未知combase.dll!_RoFailFastWithErrorContext@4()未知twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplication::ForwardLocalError(struct IRestrictedErrorInfo*)未知twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationFactory::ForwardLocalError(struct IRestrictedErrorInfo*)未知combase.dll!CallErrorForwarder(void*,int,struct IRestrictedErrorInfo*)未知combase.dll!_RoReportUnhandledError@4()未知mscorlib.ni.dll!63eb2d62()未知[下面的帧可能不正确和/或丢失,没有为mscorlib.ni.dll加载符号]
[托管到本机转换]
mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.RoReportUnhandledError(System.Runtime.InteropServices.Windows Runtime.IRestrictedErrorInfo错误)未知mscorlib.dll!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.ReportUnhandledError(System.Exception e)未知System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()未知System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeInContext(对象thisObj)未知mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext ExecutionContext,System.Threading.Context回调,对象状态,bool preserveSyncCtx)未知mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.Context回调,对象状态,bool preserveSyncCtx)未知System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.Invoke()未知[本机到托管的转换]
Windows.UI.dll!Windows::UI::Core::CDispatcher::ProcessInvokeItem()行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)行390 C++Windows.UI.Xaml.dll!DirectUI::FrameworkView::Run()未知twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationView::Run(void)未知twinapi.appcore.dll!Windows::Foundation::Collections::Internal::HashMap,structWindows::Foundation::Collections::Internal::DefaultEqualityPredicate,structWindows::Foundation:Collections::Internal::Default LifetimeTraits,struct-Windows::ApplicationModel::Core::Details::SignulatableInterfaceLifetimeTrails,struct-Windows::Foundation::Collections::Intral::HashMapOptions,0,1,0>>::Remove(unsigned int)未知SHCore.dll!Microsoft::WRL::RuntimeClass,类CScalingInfoBase,结构ICurrentWindowChangeListener,类Microsoft::WRL::FtmBase,类Microsoft::WRL::Details::Nil,类Microsoft::WRL::Details::Nil,类Microsoft::WRL::Details::Nil,类别Microsoft:::WRL::Details::Nil,阶级Microsoft::详细信息:::Nilkernel32.dll@BaseThreadInitThunk@12()未知ntdll.dll__RtlUserThreadStart()未知ntdll.dll!__RtlUserThreadStart@8()未知

这里是唯一的本地:$exception-stack

[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*

非常感谢任何帮助或指导!

我修复了它,但我不确定它是否是最佳解决方案。如果有人想建议改进,我就是游戏。

在我的GetContent()方法中,我做了以下更改:

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

相关内容

最新更新