[英]Method Error 'Cannot await 'System.Threading.Tasks.Task' from await and async properties
我已經為.NET Framework 4,Silverlight 4和5以及Windows Phone 7.5和8安裝了NuGet程序包異步。
Version 1.0.16
我正在使用Microsoft .NET 4.0,但由於Web Hosting Package限制而無法升級。 (可悲!)
我的測試代碼:(我正在用Linq to SQL查詢代替Thread.Sleep)
public class Search
{
public async Task<List<Result>> GetResults(string SearchString)
{
await System.Threading.Tasks.Task.Factory.StartNew(() => Thread.Sleep(1000));
}
}
我的錯誤:
Cannot await 'System.Threading.Tasks.Task'
http://blogs.msdn.com/b/bclteam/p/asynctargetingpackkb.aspx討論了該錯誤,但這並不是真正的答案,因為我沒有使用VB,並且已經在執行解決方案中所述的操作。
[編輯]我收到警告:
Warning 4 The primary reference "Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" or retarget your application to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
謝謝你的幫助。
什么樣的惡夢! 我發現了問題。
Microsoft.Bcl
在我看來,Microsoft.Bcl和Microsoft.Bcl.Async發生沖突。 我使用以下命令刪除了Microsoft.Bcl和Microsoft.Bcl.Async:
uninstall-package Microsoft.Bcl.Async -force
然后:
uninstall-package Microsoft.Bcl -force
然后再次安裝:
install-package Microsoft.Bcl.Async
現在,它具有魅力。 我不敢相信嗎? 還不確定為什么Microsoft.Bcl首先保留在那里。 刪除Microsoft.Bcl.Async之后,它就被留下了。 到目前為止,一切正常。 還有一些測試要做。
如果其他人對NuGet軟件包有問題,則可以自行承擔以下風險:
update-package -pre
這將嘗試回滾已更新的軟件包。 這就是我發現問題的方式。
[編輯]值得注意的是,在進一步測試之后,當卸載Microsoft.Bcl.Async時,不會刪除Microsoft.Bcl。 Microsoft.Bcl可以單獨更新,並且看來此更新不在Microsoft.Bcl.Async之外。 當安裝Microsoft.Bcl.Async時,即會安裝Microsoft.Bcl。
希望這對別人有幫助!
修改您的配置,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-[version]" newVersion="[version]" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-[version]" newVersion="[version]" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
用您正在引用的System.Runtime
和System.Threading.Tasks
的版本(例如2.5.10.0)替換[version]。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.