Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve dependency to assembly FSharp.Core 4.4.1.0 when using VS 2017

I have been developing in VS 2015 and F# 4.0 (4.4.0.0) for quite some time.

With the release of VS 2017, I want to open solutions in the newest VS for development work, but still for a while keep the projects as VS 2015, F# 4.0, .NET 4.5.2. The build server will also have to use VS 2015 for a while.

As far as I can remember, this kind of scenario has not been problematic in earlier VS version upgrades, but then I don't think I used F# at that time.

I opened the solution and tried to compile. I get this error in a C# application project. (There are other C# applications, and at least one references an F# library.)

Unknown build error, 'Cannot resolve dependency to assembly 'FSharp.Core, Version=4.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.

All my F# projects in the solution are 4.0 (4.4.0.0). I double checked.

Why is this happening?

like image 281
Bent Tranberg Avatar asked Mar 08 '17 08:03

Bent Tranberg


2 Answers

I searched for "4.4.1.0", and discovered that the "obj" folder of the C# project had a .exe.config file that differed from the app.config. It had this extra information that is not in the app.config of the project.

<runtime>
...
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.4.1.0" newVersion="4.4.1.0" />
      </dependentAssembly>
    </assemblyBinding>

Why is this appended automatically, and why only in this particular C# project?

I tried to copy that section to the app.config of the project, and change it to 4.4.0.0 in both places, but that didn't work. Also tried to use "4.4.1.0" as upper limit of old version, and have "4.4.0.0" as new version, but still didn't work. Same compiler error.

Then I removed that section, and I referenced FSharp.Core 4.4.0.0 in the C# project. That finally got rid of the compile error.

The I ran the program. It crashed with this exception.

Unhandled exception: Could not load file or assembly 'FSharp.Core, Version=4.4.1.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)

I reinserted the section with the redirect, and now the program runs fine.

Just to sum up, I added a reference to FSharp.Core 4.0, and the redirect looks like this

<bindingRedirect oldVersion="0.0.0.0-4.4.1.0" newVersion="4.4.0.0" />

With these modifications, the solution still works as expected also in VS 2015.

like image 143
Bent Tranberg Avatar answered Nov 09 '22 23:11

Bent Tranberg


I had the same problem, maybe this is helpful for someone:

In my case, the cause was that some of my C# projects with transitive dependencies on FSharp.Core were referencing the runtime's assembly installed on my system directly, instead of using the NuGet package. I.e. the reference didn't have a hint path pointing to the NuGet packages folder, and thus was picking the assembly from C:\Program Files\FSharp\... from the F# SDK. I solved this by removing the reference and reinstalling the FSharp.Core NuGet package.

So this:

<Reference Include="FSharp.Core, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />

Turns into:

<Reference Include="FSharp.Core, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\..\..\packages\FSharp.Core.4.5.2\lib\net45\FSharp.Core.dll</HintPath>
  <Private>True</Private>
</Reference>
like image 36
Efrain Avatar answered Nov 10 '22 01:11

Efrain