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

NuGet in Visual Studio 2017 doesn't add any binding redirects with SDK-style csproj format #5335

Closed
fschmied opened this issue Jun 4, 2017 · 4 comments

Comments

@fschmied
Copy link

fschmied commented Jun 4, 2017

Details about Problem

NuGet product used: VS UI
NuGet version: NuGet Package Manager 4.1.0 (integrated in VS 15.1)
dotnet.exe --version: doesn't apply
VS version: 15.1 (26403.3)
OS version: Windows 10 1607 (14393.1198)
Worked before? If so, with which NuGet version: Works in same version with old-style csproj format

Detailed repro steps so we can see the same problem

  1. Add the following SDK-style csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NUnit" Version="3.7.0" />
  </ItemGroup>

</Project>
  1. Using Visual Studio 2017, add a reference to the NuGet package "Microsoft.CodeAnalysis.Workspaces.Common", version "2.2.0". Visual Studio will not add any binding redirects. Running "Add-BindingRedirect ClassLibrary1" from the package manager console does nothing (no error output, but also no binding redirects).

  2. Add a file called Test1.cs:

using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host.Mef;
using NUnit.Framework;

namespace ClassLibrary1
{
  [TestFixture]
  public class Test1
  {
    [Test]
    public void Test ()
    {
      Console.WriteLine("Hello World!");
      var x = new AdhocWorkspace(MefHostServices.DefaultHost, "something");
      Console.WriteLine(x.Kind);
    }
  }
}
  1. Running this test throws:
System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
  ----> System.IO.FileLoadException : Could not load file or assembly 'System.Collections.Immutable, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  1. Manually adding an app.config with the binding redirects that NuGet generates for an old-style csproj fixes the problem:
<?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-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Security.Cryptography.Algorithms" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.FileVersionInfo" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Thread" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
@fschmied
Copy link
Author

fschmied commented Jun 4, 2017

@rrelyea This might be related to #3372, though it's SDK-style csproj rather than project.json.

@dasMulli
Copy link

dasMulli commented Jun 5, 2017

If this is a test project, that should be fixed by microsoft/vstest#642.

The reason is that binding redirects are generated automatically during build for all "executable" projects. But since a full framework test project is a library, no binding redirects are generated. The Microsoft.NET.Test.Sdk >= 15.3.* set the right properties to enable generation of binding redirects.

The generation of binding redirects is currently only enabled by default for Exe/WinExe output types.

@fschmied
Copy link
Author

fschmied commented Jun 6, 2017

@dasMulli You're right, thanks - referencing the Microsoft.NET.Test.Sdk 15.3.0-preview-20170601-03 NuGet package made the problem go away. I also found https://stackoverflow.com/a/42806831/62838, which provides a different workaround (manually adding the corresponding build properties).

@fschmied fschmied closed this as completed Jun 6, 2017
@alancapc
Copy link

@dasMulli, you are absolutely right. Binding redirects is an issue when changing targets as well.

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

3 participants