How to use the Microsoft.Bcl.Async right?
.net-4.0, async-await, asynchronous, c#, wpf
Solution
Summary:
There's a couple of workarounds available to you:
Install the Microsoft.Bcl.Async package to the referencing project.
Install the Microsoft.Bcl.Build package 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.
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.
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.
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.
Problem
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. 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". I use in both projects this 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. I can't use .Net 4.5 target. It must be .Net 4. Target Framework for all projects: .NET Framework 4