2014-02-13 77 views
1

我正在爲Windows存儲構建電子書管理器,並在其中一個類上實現了IUriToStreamResolver接口。我正在使用它來打開epub。代碼如下:System.InvalidCast異常Windows運行時

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(長,無符號長,結構_STOWED_EXCEPTION_INFORMATION_V1 *常量)未知 [email protected]()未知 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 *)未知 [email protected]()未知 mscorlib.ni.dll!63eb2d62()未知 [下面的框架可能不正確和/或丟失,沒有爲mscorlib.ni.dll加載的符號]
[Managed to Native過渡]
mscorlib.dll中!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.RoReportUnhandledError(System.Runtime.InteropServices.WindowsRuntime.IRestrictedErrorInfo錯誤)未知 mscorlib.dll中!System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.ReportUnhandledError(系統.Exception e)未知 System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()未知 System.Runtime.WindowsRuntime.dll!System.Threading.WinRTSynchronizationContext.Invo ker.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()未知 twinapi.appcore.dll!Windows :: ApplicationModel :: Core :: CoreApplicationView :: Run(void)未知 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 >> ::刪除(unsigned int)未知 SHCore.dll!Microsoft :: WRL :: RuntimeClass,類CScalingInfoBase,結構ICalWindowChangeListener,類Microsoft :: WRL :: FtmBase,類Microsoft :: WRL :: Details :: Nil,類Microsoft :: WRL :: Details :: Nil,class Microsoft: :WRL ::詳細信息::無,類Microsoft :: WRL ::詳細信息::無,類Microsoft :: WRL ::詳細信息::無,類Microsoft :: WRL ::詳細信息:: Nil> ::刪除向量析構函數「(無符號整數)未知 KERNEL32.DLL!@ BaseThreadInitThunk @ 12()未知 ntdll.dll的!__ RtlUserThreadStart()未知 ntdll.dll的!__ RtlUserThreadStart @ 8()未知

這裏是唯一local: $ exceptionstack

[9 Frames, [email protected]()] 
    [0] [email protected]() 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* 

任何幫助或方向是非常感謝!

+0

我敢打賭這是你stream.AsStream使用'()',因爲這是在'WindowsRuntime Interop'的擴展之一。我認爲'StorageFile'應該有自己的擴展名,比如'file.OpenStreamForReadAsync()'或'stream.AsStreamForRead()'。 –

+0

我已經嘗試了將編譯的那些擴展方法的每個組合,並且我得到相同的結果。 –

+0

您是否有機會嘗試訪問OneDrive中的文件(SkyDrive的新名稱)? –

回答

0

我修正了它,但我不確定它是否是最佳解決方案。如果有人想提出改進建議,我就是遊戲。

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