28

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?

Bent Tranberg
  • 3,445
  • 26
  • 35

3 Answers3

18

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.

Bent Tranberg
  • 3,445
  • 26
  • 35
  • The above problem was in a C# program that used F# libraries. Today I had a similar problem with an F# program using F# 4.0.0.0 in VS 2017. There was no compile error, only the above runtime error. I solved it by using bindingRedirect, as explained here. I do not understand why this only occurs for some of my programs. – Bent Tranberg Mar 12 '17 at 12:39
  • 1
    Seems that I am unable to inspect expressions during debugging with this. Adding a watch shows the error: error CS1705: Assembly 'x' with identity 'x' uses 'FSharp.Core, Version=4.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'FSharp.Core' with identity 'FSharp.Core, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' – marknuzz Apr 21 '17 at 21:07
  • Funny how I keep getting back to my own solution over and over and over again. I've lost count. This time it was "The application is in break mode / Type initialization exception was unhandled" at startup, before it even gets to main, and I've spent 1.5 hours removing every other possible source before landing here ... again. This happens when using FsXaml. The problem was gone a few months, but came back like a boomerang. Phew! – Bent Tranberg Nov 23 '17 at 21:31
  • 2
    NOTE: If you are at .NET Framework 4.6.* and F# 4.1, your problem could simply be that you need to add a reference to FSharp.Core 4.4.1.0 (Not Microsoft.FSharp...) in your C# project(s) where this error appears. – Bent Tranberg Jan 14 '18 at 06:43
2

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>
Efrain
  • 3,248
  • 4
  • 34
  • 61
-2

Check the assembly references of the assembly in the message. For me I had a reference to assembly X which had a reference to Y. Because Y was missing, I got this error. By referencing Y, the error was resolved for me.

Wesley Kenis
  • 107
  • 4