Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use app.config file with a C++/CLI to configure assembly binding in VS 2012?

I am currently using the Microsoft BCL Async library (here) across a project with a large number of interdependent assemblies all compiled against .NET 4 Full Profile, I have had to use assembly binding redirect in each project to get it to compile (as per issue 2 here).

The problem I now have is that I consume one of these libraries from a C++/CLR DLL project, it does not actually need to use async but I have the following compilation error:

2>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1578,5): warning MSB3268: The primary reference "ImInterface.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 "ImInterface.dll" or retarget your application to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

I have added the same app.config file that I have used in all of the C# projects but it does not seem to be having any effect in the C++ project. Do I have to place it in a specific directory or perform any other steps to enable the app.config file to be recognised?

The content of my app.config file is:

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

Any suggestions are appreciated!

Regards, Anthony

like image 385
Anthony Avatar asked Dec 14 '12 14:12

Anthony


People also ask

How do I add-BindingRedirect?

Open the NuGet Package Manager Console, specify the appropriate Default Project and enter the command Add-BindingRedirect. As if by magic, an app. config is added to the project (if one doesn't exist already) and the appropriate information added.

How do I add assembly redirects?

To redirect one assembly version to another, use the <bindingRedirect> element. The oldVersion attribute can specify a single assembly version or a range of versions. The newVersion attribute should specify a single version.

How do I enable and disable automatic binding redirection?

If you have Visual Studio 2017 version 15.7 or later, you can disable autogenerated binding redirects in the project's property pages. Right-click the project in Solution Explorer and select Properties. On the Application page, uncheck the Auto-generate binding redirects option.


1 Answers

I had the same problem and as it turned out, <SpecificVersion>True</SpecificVersion> helped.

Before you continue reading, note that the problem seems to only happen when the C++/CLI project is built using the "Visual Studio 2012 - Windows XP (v110_xp)" platform toolset! That is if you don't need to create a DLL which is compatible with WinXP, switch to "Visual Studio 2012 (v110)" instead and you should be fine.

Let me elaborate my scenario:

I have a C++/CLI console application project (ConsoleAppCLI) targeted for WinXP (v110_xp). ConsoleAppCLI references a library (AsyncLib) which has Microsoft.Bcl.Async installed via NuGet:

[ConsoleAppCLI.vcxproj]   (C++/CLI, .NET 4.0 v110_xp)
          |
          |
   [AsyncLib.csproj]      (C#, .NET 4.0)
          |
          |
 [Microsoft.Bcl.Async]    (via NuGet)

I had the exact same problem and the app.config didn't help either, but what did help was the following:

  1. Unload ConsoleAppCli.vcxproj
  2. Edit the vcxproj File directly
  3. Find the reference to the library which has the Async nuget package installed (in my case, a <ProjectReference> to AsyncLib.csproj)
  4. Between <ProjectReference> and </ProjectReference>, ADD THE FOLLOWING: <SpecificVersion>True</SpecificVersion>

Note however, that ConsoleAppCLI was my top-level project in the dependency tree. If you have Assemblies which depend upon the C++/CLI assembly which produced MSB3268, you likewise have to add <SpecificVersion>True</SpecificVersion> to the reference (e.g. if i had an additional C# library which has ConsoleAppCli as dependency, i would have to make the reference to ConsoleAppCli <SpecificVersion>True</SpecificVersion> as well).

This solution is coming from over here, by the way.

like image 74
somebloke Avatar answered Oct 23 '22 15:10

somebloke