2016-11-24 41 views
0

下面這個回報是相關代碼:什麼是錯的,從一個異步函數

Protected Async Function GetMonthlyBillsAsync(month As Date) As Task(Of IDictionary(Of String, Lazy(Of iBill))) 
    Dim invoiceNumbers = New Dictionary(Of String, Lazy(Of PlumbersSuppliesCoOpBill)) 
'Stuff to fill the dictionary which works giving 66 elements 
    Return invoiceNumbers 
End Function 

從名爲:

Dim thisMonth = Await GetMonthlyBillsAsync(sd) 

當return語句執行我得到一個的NullReferenceException有:

System.NullReferenceException was unhandled 

    HResult=-2147467261 

    Message=Object reference not set to an instance of an object. 

    Source=ConsoleApplication1 

    StackTrace: 

    at ConsoleApplication1.Module1.Main() in \\fileserver\data\Users\Dale\Visual Studio Projects\SupplierBills\ConsoleApplication1\Module1.vb:line 7 

    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 

    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 

    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 

    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 

    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 

    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 

    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 

    at System.Threading.ThreadHelper.ThreadStart() 

    InnerException: 

這是怎麼回事?

+0

你是說你的邏輯沒有異步/等待關鍵字? – anthonybell

+0

好問題 - 我會檢查 –

回答

0

好的,答案是我沒有注意並且發現了錯誤的異常。

正確的例外是:

無法轉換詞典(串,懶惰(中PlumbersSuppliesCoOpBill))到IDictionary的(字符串,懶惰(中iBill))

這使得完美正如在Error: Can't convert from Dictionary to IDictionary中所解釋的那樣 - 它不是Dictionary to iDictionary就是問題 - 它是PlumbersSuppliesCoOpBill to iBill內的詞典。

我將返回值的類型更改爲Dictionary(Of String,Lazy(Of iBill))並且它有效。

-1

您已經聲明如下返回類型: 任務(中的IDictionary(字符串,懶惰(中iBill)))

但是你返回的功能類型是: 詞典(串,懶惰(的水管工供應合同費))

你可以清楚地看到類型是不同的,這就是爲什麼它打破。

將返回類型與您聲明的類型進行匹配可以解決您的問題。

謝謝

相關問題