wpf 如何正确使用 Microsoft.Bcl.Async?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15551953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the Microsoft.Bcl.Async right?
提问by punker76
I use the Microsoft.Bcl.Asyncpackagein a project and this project is a referenced by another project that does not use async features.
我在一个项目中使用了这个Microsoft.Bcl.Async包,这个项目被另一个不使用异步功能的项目引用。
Now I get this error warning when I compile the solution (or only the second project):
现在,当我编译解决方案(或仅编译第二个项目)时收到此错误警告:
The primary reference "XYZ.dll" 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 "XYZ.dll" or retarget your application to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
无法解析主要引用“XYZ.dll”,因为它间接依赖于框架程序集“System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”,在当前目标中无法解析框架。“.NETFramework,版本 = v4.0”。要解决此问题,请删除引用“XYZ.dll”或将您的应用程序重定向到包含“System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的框架版本。
I use in both projects this app.config:
我在这两个项目中都使用了这个 app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:bcl="urn:schemas-microsoft-com:bcl">
<dependentAssembly bcl:name="System.Runtime">
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.16.0" newVersion="2.5.16.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.16.0" newVersion="2.5.16.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
What I'm doing wrong?
我做错了什么?
I don't want to reference the async package dlls.
我不想引用异步包 dll。
I can't use .Net 4.5 target. It must be .Net 4.
我不能使用 .Net 4.5 目标。它必须是 .Net 4。
Target Framework for all projects: .NET Framework 4
所有项目的目标框架:.NET Framework 4
采纳答案by David Kean
Summary:
总结:
There's a couple of workarounds available to you:
有几种解决方法可供您使用:
Install the Microsoft.Bcl.Asyncpackage to the referencing project.
Install the Microsoft.Bcl.Buildpackage to the referencing project. This contains the actual fix, and is solely a build-time dependency. When you do this, you must have the appropriate binding redirects in the project's App.Config (such as what you've listed above) - regardless of whether it is a class library, web project or an executable.
If you don't want a dependency on any package, grab the contents of the PropertyGroup element within Microsoft.Bcl.targets file (installed with Microsoft.Bcl.Build), and insert it at the bottom of the referencing project after the last Import element.
将Microsoft.Bcl.Async包安装到引用项目。
将Microsoft.Bcl.Build包安装到引用项目。这包含实际修复,并且仅是构建时依赖项。执行此操作时,您必须在项目的 App.Config 中具有适当的绑定重定向(例如上面列出的内容)——无论它是类库、Web 项目还是可执行文件。
如果您不想依赖任何包,请获取 Microsoft.Bcl.targets 文件(随 Microsoft.Bcl.Build 一起安装)中的 PropertyGroup 元素的内容,并在最后一次导入后将其插入到引用项目的底部元素。
Note:Which ever option you choose above, when you ship a library that takes a dependency on Microsoft.Bcl.Async (as with the old Microsoft.CompilerServices.AsyncTargetingPack), you must ship those binaries (System.Runtime, System.Threading.Tasks, Microsoft.Threading.Tasks.*) with the application/package that uses your library.
注意:无论您选择上面哪个选项,当您发布依赖于 Microsoft.Bcl.Async 的库(与旧的 Microsoft.CompilerServices.AsyncTargetingPack 一样)时,您必须发布这些二进制文件(System.Runtime、System.Threading。 Tasks、Microsoft.Threading.Tasks.*) 与使用您的库的应用程序/包。
Long story:
长话短说:
As Peter pointed out this is a known issue that only occurs for Microsoft.Bcl.Async and not Microsoft.CompilerServices.AsyncTargetingPack, due to the way they are designed.
正如 Peter 指出的那样,这是一个已知问题,只发生在 Microsoft.Bcl.Async 而不是 Microsoft.CompilerServices.AsyncTargetingPack,因为它们的设计方式。
Part of the design of Microsoft.Bcl.Async was to backport (via a NuGet package) some new .NET 4.5 assemblies (System.Runtime, System.Threading.Tasks) so that they would run on 4.0. MSBuild does not like this, and it causes it to believe that the referencing library has taken a dependency on an assembly from a newer framework version. The workaround in Microsoft.Bcl.Build package fixes this.
Microsoft.Bcl.Async 的部分设计是向后移植(通过 NuGet 包)一些新的 .NET 4.5 程序集(System.Runtime、System.Threading.Tasks),以便它们可以在 4.0 上运行。MSBuild 不喜欢这样,它导致它相信引用库依赖于来自较新框架版本的程序集。Microsoft.Bcl.Build 包中的解决方法修复了这个问题。
回答by Peter Ritchie
Use Async Targeting Pack for Visual Studio 11instead.
请改用适用于 Visual Studio 11 的异步目标包。
Install-Package Microsoft.CompilerServices.AsyncTargetingPack
Install-Package Microsoft.CompilerServices.AsyncTargetingPack
If you prefer to use the Microsoft.Bcl.Async prerelease, you'll have to add it to all the projects. Next release of Bcl.Async should have improved messaging to better detail that.
如果您更喜欢使用 Microsoft.Bcl.Async 预发行版,则必须将其添加到所有项目中。Bcl.Async 的下一个版本应该改进消息传递以更好地详细说明。
If you're using VS 2010, the only thing that I know of that gives you async/wait is the AsyncCTP; so, it doesn't make sense to use Microsoft.Bcl.Async in VS 2010.
如果您使用的是 VS 2010,我所知道的唯一能让您异步/等待的是 AsyncCTP;所以,在 VS 2010 中使用 Microsoft.Bcl.Async 是没有意义的。
update:
更新:
W.R.T to Microsoft.Bcl.Async: The issue around the MSB warning ("cannot be resolved...indirect dependency..." is known and being addressed. In some future update (to MSBuild and Microsoft.Bcl.Async) this will be fixed and you won'thave to include Microsoft.Bcl.Async in both projects.
WRT 到 Microsoft.Bcl.Async:围绕 MSB 警告的问题(“无法解决...间接依赖...”是已知的并正在解决。在未来的一些更新中(对 MSBuild 和 Microsoft.Bcl.Async)这将得到修复,您不必在两个项目中都包含 Microsoft.Bcl.Async。

