17

I have a WPF (.NET 4) project using google url shortener API, I have installed the client library through nugget https://www.nuget.org/packages/Google.Apis.Urlshortener.v1/1.7.0.25-beta

the application works fine in visual studio but once published it throws the exception Could not load file or assembly System.Threading.Tasks, Version=2.5.19.0 this and all other assemblies are present in the installation folder, and it gets publish with application. I have searched internet and people suggest to manually bind the dependency libraries in the app.config, it still does not work as all of my dependency libraries are already mentioned in app.config, below is how my app.config looks like

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.0.165.0" newVersion="1.0.165.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Syed Waqas
  • 862
  • 2
  • 9
  • 29
  • I am having an identical issue. Were you able to resolve your issue, and if so, how? – MatthewT Feb 03 '14 at 07:25
  • 2
    i solve it by updating google libraries to the recent ones and installing .net 4.5, it was always working on my development machines as it has .net 4.5, I install it on clients computer and remove the erroneous dependency assembly from app.config – Syed Waqas Feb 03 '14 at 09:52

3 Answers3

14

You might start from Microsoft BCL team blog and clean up the app.config by removing the wrong entries,

http://blogs.msdn.com/b/bclteam/p/asynctargetingpackkb.aspx (archive)

Issue 6

Symptoms

When adding the NuGet package to a project that is consumed by another project with a different target framework you might see warnings similar to the following:

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=2.5.19.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.5". 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=2.5.19.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

The primary reference "Microsoft.Threading.Tasks.Extensions, 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=2.5.19.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.5". To resolve this problem, either remove the reference "Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" or retarget your application to a framework version which contains "System.Runtime, Version=2.5.19.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

Solution

The problem is that NuGet added incorrect binding redirects for platform assemblies. To remove them, please open the app.config for the project that caused the warnings and remove the highlighted entries [noted by *****]:

<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
    <runtime>
        <assemblyBindingxmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>******
                <assemblyIdentityname="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                <bindingRedirectoldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
            </dependentAssembly> .
            <dependentAssembly>******
                <assemblyIdentityname="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                <bindingRedirectoldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
            </dependentAssembly>
            </assemblyBinding>
    </runtime>
</configuration>

Commentary:

With .NET Framework 4.0 end-of-life, you should think twice before using the async targeting pack yourself. If this dependency comes from a NuGet package, you should also check whether the NuGet package has a newer version that has no such dependency.

Monasha
  • 882
  • 1
  • 12
  • 17
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 1
    i followed the instructions after removing the highlighted assemblies now i get Could not load file or assembly System.Threading.Tasks, Version=1.5.11.0 – Syed Waqas Jan 19 '14 at 10:18
  • 3
    Check issue 9 of the same article. – Lex Li Jan 19 '14 at 13:43
  • How would I accomplish the "Add as Link" in the solution for issue 9 if I am using the installer project instead of ClickOnce? I am targeting .net 4.0 if that is relevant. – Spidermain50 Dec 19 '16 at 16:31
  • @Carl The discussion was carried out in 2014. After that Microsoft did make improvements in newer .NET Framework releases to address such issues like written in [the release notes](https://learn.microsoft.com/en-us/dotnet/framework/whats-new/#base-classes-2) – Lex Li Jul 27 '21 at 17:48
  • Sure, but I haven't found a good way to determine which of the 100 packages in use has a dependency on the task package. I don't have time to upgrade and test each one. For anyone else in my position - getting rid of the assembly binding for System.Runtime made the difference – Carl Jul 28 '21 at 08:51
1

I had a very similar issue ("Could not load file or assembly Microsoft.Threading.Tasks, Version=1.0.12.0") in a UWP project (VS2015) and I solved it by installing the Microsoft.Bcl.Async package from NuGet

Detail
  • 785
  • 9
  • 23
0

I had the exact same problem but it was caused by the assembly Microsoft.Rest.ClientRuntime. In my case all I had to do was to set "Copy local=True" on the reference to Microsoft.Rest.ClientRuntime.

Björn
  • 3,098
  • 2
  • 26
  • 40