简体   繁体   English

如何使用Microsoft.Bcl.Async吧?

[英]How to use the Microsoft.Bcl.Async right?

I use the Microsoft.Bcl.Async package in 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. 无法解析主要引用“XYZ.dll”,因为它对框架程序集“System.Runtime,Version = 1.5.11.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”具有间接依赖性,无法在当前目标中解析框架。 ".NETFramework,Version=v4.0". ” .NETFramework,版本= 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”的框架版本。

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. 我不能使用.Net 4.5目标。 It must be .Net 4. 它必须是.Net 4。

Target Framework for all projects: .NET Framework 4 所有项目的目标框架:.NET Framework 4

Summary : 摘要

There's a couple of workarounds available to you: 您可以使用以下几种解决方法:

  1. Install the Microsoft.Bcl.Async package to the referencing project. Microsoft.Bcl.Async程序包安装到引用项目。

  2. Install the Microsoft.Bcl.Build package to the referencing project. Microsoft.Bcl.Build包安装到引用项目。 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. 执行此操作时,您必须在项目的App.Config中具有相应的绑定重定向(例如上面列出的内容) - 无论它是类库,Web项目还是可执行文件。

  3. 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.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。任务,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. 正如彼得所指出的,这是一个已知的问题,只有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. Microsoft.Bcl.Async的部分设计是向后端口(通过NuGet包)一些新的.NET 4.5程序集(System.Runtime,System.Threading.Tasks),以便它们可以在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. MSBuild不喜欢这样,它使它相信引用库已经从较新的框架版本依赖于程序集。 The workaround in Microsoft.Bcl.Build package fixes this. Microsoft.Bcl.Build包中的解决方法修复了此问题。

Use Async Targeting Pack for Visual Studio 11 instead. 请改用Visual Studio 11的Async Targeting Pack

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. 如果您更喜欢使用Microsoft.Bcl.Async预发行版,则必须将其添加到所有项目中。 Next release of Bcl.Async should have improved messaging to better detail that. Bcl.Async的下一个版本应该改进消息传递以更好地详细说明。

If you're using VS 2010, the only thing that I know of that gives you async/wait is the AsyncCTP; 如果您使用的是VS 2010,那么我所知道的唯一能让您同步/等待的是AsyncCTP; so, it doesn't make sense to use Microsoft.Bcl.Async in VS 2010. 因此,在VS 2010中使用Microsoft.Bcl.Async是没有意义的。

update: 更新:

WRT 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't have to include Microsoft.Bcl.Async in both projects. WRT to Microsoft.Bcl.Async:关于MSB警告的问题(“无法解决...间接依赖...”已知并正在解决。在将来的某个更新中(对于MSBuild和Microsoft.Bcl.Async),这将是修复后,您不必在两个项目中都包含Microsoft.Bcl.Async。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM