Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net40 application with Microsoft.Bcl.Async cannot resolve dependencies #7895

Open
Mats391 opened this issue Mar 10, 2017 · 21 comments
Open

net40 application with Microsoft.Bcl.Async cannot resolve dependencies #7895

Mats391 opened this issue Mar 10, 2017 · 21 comments
Milestone

Comments

@Mats391
Copy link

Mats391 commented Mar 10, 2017

Steps to reproduce

Create a net40 application that references Microsoft.Bcl.Async and uses await:
project.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net40</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Bcl.Async" Version="1.0.168" />
  </ItemGroup>

</Project>

Program.cs:

using System.Threading.Tasks;

class Program
{
    static void Main() => F().Wait();
    static async Task F() => await TaskEx.Delay(100);
}

Start with dotnet run

Expected behavior

Program should start.

Actual behavior

Creates errors

C:\Program Files\dotnet\sdk\1.0.0\Microsoft.Common.CurrentVersion.targets(1964,5): warning MSB3268:
The primary reference "C:\Users\Schoenke\.nuget\packages\microsoft.bcl.async\1.0.168\lib\net40\Micro
soft.Threading.Tasks.dll" could not be resolved because it has an indirect dependency on the framewo
rk assembly "System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50
a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0".
To resolve this problem, either remove the reference "C:\Users\Schoenke\.nuget\packages\microsoft.bc
l.async\1.0.168\lib\net40\Microsoft.Threading.Tasks.dll" or retarget your application to a framework
 version which contains "System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b
03f5f7f11d50a3a". [C:\Users\Schoenke\Documents\Visual Studio 2017\Projects\NetCoreBCLTest\NetCoreBCL
Test\NetCoreBCLTest.csproj]
C:\Program Files\dotnet\sdk\1.0.0\Microsoft.Common.CurrentVersion.targets(1964,5): warning MSB3268:
The primary reference "C:\Users\Schoenke\.nuget\packages\microsoft.bcl.async\1.0.168\lib\net40\Micro
soft.Threading.Tasks.Extensions.dll" could not be resolved because it has an indirect dependency on
the framework assembly "System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b0
3f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Vers
ion=v4.0". To resolve this problem, either remove the reference "C:\Users\Schoenke\.nuget\packages\m
icrosoft.bcl.async\1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll" or retarget your appl
ication to a framework version which contains "System.Threading.Tasks, Version=1.5.11.0, Culture=neu
tral, PublicKeyToken=b03f5f7f11d50a3a". [C:\Users\Schoenke\Documents\Visual Studio 2017\Projects\Net
CoreBCLTest\NetCoreBCLTest\NetCoreBCLTest.csproj]
C:\Program Files\dotnet\sdk\1.0.0\Microsoft.Common.CurrentVersion.targets(1964,5): warning MSB3268:
The primary reference "C:\Users\Schoenke\.nuget\packages\microsoft.bcl.async\1.0.168\lib\net40\Micro
soft.Threading.Tasks.dll" could not be resolved because it has an indirect dependency on the framewo
rk assembly "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" whi
ch could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resol
ve this problem, either remove the reference "C:\Users\Schoenke\.nuget\packages\microsoft.bcl.async\
1.0.168\lib\net40\Microsoft.Threading.Tasks.dll" or retarget your application to a framework version
 which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
. [C:\Users\Schoenke\Documents\Visual Studio 2017\Projects\NetCoreBCLTest\NetCoreBCLTest\NetCoreBCLT
est.csproj]
C:\Program Files\dotnet\sdk\1.0.0\Microsoft.Common.CurrentVersion.targets(1964,5): warning MSB3268:
The primary reference "C:\Users\Schoenke\.nuget\packages\microsoft.bcl.async\1.0.168\lib\net40\Micro
soft.Threading.Tasks.Extensions.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=b03f5f7f11
d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0
". To resolve this problem, either remove the reference "C:\Users\Schoenke\.nuget\packages\microsoft
.bcl.async\1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll" or retarget your application
to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyT
oken=b03f5f7f11d50a3a". [C:\Users\Schoenke\Documents\Visual Studio 2017\Projects\NetCoreBCLTest\NetC
oreBCLTest\NetCoreBCLTest.csproj]
Program.cs(9,40): error CS0103: The name 'TaskEx' does not exist in the current context [C:\Users\Sc
hoenke\Documents\Visual Studio 2017\Projects\NetCoreBCLTest\NetCoreBCLTest\NetCoreBCLTest.csproj]

The build failed. Please fix the build errors and run again.

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.0)

Product Information:
 Version:            1.0.0
 Commit SHA-1 hash:  e53429feb4

Runtime Environment:
 OS Name:     Windows
 OS Version:  6.1.7601
 OS Platform: Windows
 RID:         win7-x86
 Base Path:   C:\Program Files\dotnet\sdk\1.0.0

Side Note:
Similar behaviour with Microsoft.Net.Http which fails to resolve System.Net.Http.

@livarcocc
Copy link
Contributor

@dsplaisted can you help out here?

@chrishaly
Copy link

same issue

@blackcity
Copy link

Same here. We switched back to VS2015 because we need to support NET4 targets.

@MichaCo
Copy link

MichaCo commented Mar 20, 2017

Same issue, I cannot use the StackExchange.Redis client in my library targeting 4.0, and 4.5 and netstandard...

@xrkolovos
Copy link

same here, any workaround?

@rguryanov
Copy link

rguryanov commented Apr 9, 2017

One workaround for that problem.
Add import to Microsoft.Bcl.Build.targets in .csproj:
<Import Project="..\Microsoft.Bcl.Build.targets" Condition="Exists('..\Microsoft.Bcl.Build.targets')" />
File Microsoft.Bcl.Build.targets can be found in packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets when you install Microsoft.Bcl.Async nuget in classic windows desktop app.

And add App.config to project (in vs add-> new item-> application configuration file):

<?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-2.6.8.0" newVersion="2.6.8.0" />
			</dependentAssembly>
			<dependentAssembly>
				<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
				<bindingRedirect oldVersion="0.0.0.0-2.6.8.0" newVersion="2.6.8.0" />
			</dependentAssembly>
		</assemblyBinding>
	</runtime>
</configuration>

When you target multiple frameworks do remember to change condition for import and App.config. For example to something as

<Import Project="..\Microsoft.Bcl.Build.targets" Condition="Exists('..\Microsoft.Bcl.Build.targets') AND '$(TargetFramework)' == 'net40'" />
<ItemGroup Condition="'$(TargetFramework)' != 'net40'">
	<None Remove="app.config" />
</ItemGroup>

@Mats391
Copy link
Author

Mats391 commented Apr 18, 2017

@rguryanov This does not work for me. Maybe I am doing something wrong. Could you explain where to put the .targets file etc. Maybe post example .csproj. Thanks a lot.

Edit: I think my issue is that I have the BCL stuff in a class library and the workaround does not seem to work for those.

@canton7
Copy link

canton7 commented Apr 18, 2017

I also have class library which uses Microsoft.Bcl.Async. I also tried @rguryanov's instructions, and also couldn't get them to work (the version of System.Threading.Tasks which it claimed a dependency on changed according to the binding redirect, but the rest of the error stayed the same).

@rguryanov
Copy link

@Mats391 @canton7
Simple example of workaround https://github.com/rguryanov/dotnet-cli-5993
You only need add app.config for console app to work.

@canton7
Copy link

canton7 commented Apr 18, 2017

@rguryanov thanks! That's for a console app. The challenge we have is making something similar work for a class library.

@rguryanov
Copy link

rguryanov commented Apr 18, 2017

@canton7
Issue have been started for console application.
Workaround for class library would be something like https://github.com/rguryanov/dotnet-cli-5993-multitarget
you need additionally add import to Microsoft.Bcl.Build.targets from Microsoft.Bcl.Build package (and build process depends on libs from package, so copy package in your solution dir) in both class library and app.

@Mats391
Copy link
Author

Mats391 commented Apr 19, 2017

@rguryanov Thanks a lot, that works!
You dont happen to have a solution for the same issue with Microsoft.Net.Http lying around, do you? :)
Warning MSB3268 The primary reference "C:\Users\Schoenke\.nuget\packages\microsoft.net.http\2.2.29\lib\net40\System.Net.Http.Extensions.dll" could not be resolved because it has an indirect dependency on the framework assembly "System.Net.Http, Version=1.5.0.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 "C:\Users\Schoenke\.nuget\packages\microsoft.net.http\2.2.29\lib\net40\System.Net.Http.Extensions.dll" or retarget your application to a framework version which contains "System.Net.Http, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
I suppose it would be similar solution, but just adding the redirect to the app.config does not solve it.

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.0.0" newVersion="1.5.0.0" />
</dependentAssembly>

Solved it by targeting Microsoft.BCL.Build 1.0.21 and changing app.config to

<?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-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

@karelz
Copy link
Member

karelz commented May 1, 2017

I wonder if the DLL ships out of CoreFX? (@danmosemsft @weshaggard)
Is there anything we can help with from CoreFX side? @ericstj @weshaggard ... at minimum I expect we have expertise on the problem space ...

cc @dsplaisted

@terrajobst
Copy link
Contributor

No, those libraries shipped out of our old Roxel tree. We've published the source code as part of reference source.

I'm not entirely sure why this doesn't work. I'll take a look. Setting AGBR should have fixed the issue but it doesn't.

@dsplaisted
Copy link
Member

dsplaisted commented May 3, 2017

It turns out that the Microsoft.Bcl.Build has a targets file which adds the right binding redirects which is added to the project via Install.ps1. So it doesn't work with PackageReference.

To work with PackageReference, the package would need to be updated to use the build folder package convention to hook up the targets file. Further changes would also be needed to support building using .NET Core MSBuild as opposed to full framework MSBuild.

@terrajobst, @ericstj, @weshaggard, thoughts on this?

@xrkolovos
Copy link

Any news for any workaround on this issue?

@ericstj
Copy link
Member

ericstj commented Jan 30, 2018

To work with PackageReference, the package would need to be updated to use the build folder package convention to hook up the targets file. Further changes would also be needed to support building using .NET Core MSBuild as opposed to full framework MSBuild.

It already does this in the latest version.

@pizixie
Copy link

pizixie commented Apr 2, 2018

@rguryanov your example maybe work when OutputType is Exe,but Library did not

this problem had be solved in this by @ericstj

just to add

<AppConfig Condition="'$(TargetFramework)' == 'net40'">app.net40.config</AppConfig>
<AutoUnifyAssemblyReferences Condition="'$(TargetFramework)' == 'net40'">false</AutoUnifyAssemblyReferences>

app.net40.config like below:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

@mark0790
Copy link

For library assemblies, just set the app.config to output as "Content"

@msftgits msftgits transferred this issue from dotnet/cli Jan 31, 2020
@msftgits msftgits added this to the Backlog milestone Jan 31, 2020
@HavenDV
Copy link

HavenDV commented Apr 26, 2020

Alternative solution. Used it to fix problems with finding dll dependencies (after regasm and when calling from a vbs file via COM):

using System;
using System.IO;
using System.Reflection;

#nullable enable

namespace Utilities
{
    public static class XpUtilities
    {
        private static bool IsFixed { get; set; }

        public static void FixAsyncAssemblyResolve()
        {
            if (IsFixed)
            {
                return;
            }

            IsFixed = true;
            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
        }

        private static Assembly? OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            switch (args.Name)
            {
                case "System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a":
                    var path = Assembly.GetExecutingAssembly().Location;
                    var folder = Path.GetDirectoryName(path);

                    return folder == null
                        ? null
                        : Assembly.LoadFrom(Path.Combine(folder, "System.Threading.Tasks.dll"));

                default:
                    return null;
            }
        }
    }
}

@mzettwitz
Copy link

Just to mention because I stumbled across this problem: you need to copy the *.exe.config file from the build folder into your desired output folder, too. Otherwise, the .dlls could not be referenced even when you performed the steps above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests