从异步函数返回该错误有什么问题

时间:2016-11-24 01:03:23

标签: .net vb.net function asynchronous return

以下是相关代码:

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: 

发生了什么事?

2 个答案:

答案 0 :(得分:0)

好的,答案是我没有注意并抓住了错误的例外。

正确的例外是:

  

无法将Dictionary(Of String,Lazy(Of PlumbersSuppliesCoOpBill))转换为iDictionary(Of String,Lazy(Of iBill))

Error: Can't convert from Dictionary to IDictionary中所解释的那样完全正确 - 它不是iDictionary的词典,而是字典中的问题 - 它是字典中的PlumberSuppliesCoOpBill到iBill。

我将返回值的类型更改为Dictionary(Of String,Lazy(Of iBill))并且它有效。

答案 1 :(得分:0)

您已声明返回类型如下: 任务(IDictionary(Of String,Lazy( of iBill )))

但是你返回的函数类型是: 字典(字符串,懒惰( PlumbersSuppliesCoOpBill ))

你可以清楚地看到类型不同,这就是它破裂的原因。

将返回类型与声明的类型匹配可以解决您的问题。

由于