2016-11-24 40 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)))

但是你返回的功能类型是: 词典(串,懒惰(的水管工供应合同费))

你可以清楚地看到类型是不同的,这就是为什么它打破。

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

谢谢

相关问题