Rich Turner
Rich Turner

Reputation: 10984

How do I add a reference to F# Portable Library from C# Portable Class Library (PCL)

I have a project which contains two F# projects and a C# project in which I'd like to write some XUnit tests:

I am unable to add a reference from Tests to either of the F# libraries.

When I try to add a reference to FS_PL, I am presented with a dialog that states "Unable to add a reference to project 'FS_PL'. The targets of Portable Library project 'FS_PL' are not the same or compatible with the targets of the current Portable Library project": enter image description here

This is odd since both my Tests and FS_PL libraries are configured to target .NET 4.5 & Windows 8.

So I created FS_PL_Legacy and tried adding a reference to it. Doing so gives me a very 'helpful' message stating "Unable to add a reference to project 'FS_PL_Legacy'": enter image description here

Does anyone know what I am doing wrong?

Partial Workaround #1

Using miegirl's workaround from a Connect issue discussing this problem, I added the following to the C# project:

<ItemGroup>
    <!-- Manually added reference to F# projects to overcome issue discussed here:
            http://stackoverflow.com/questions/23111782/how-do-i-add-a-reference-to-f-portable-library-from-c-sharp-portable-class-libr
        -->
    <ProjectReference Include="..\FS_PL\FS_PL.fsproj">
        <Project>{2c4b1776-3d34-4534-8520-8a1e6daa0e6e}</Project>
        <Name>FS_PL</Name>
    </ProjectReference>
    <ProjectReference Include="..\FS_PL_Legacy\FS_PL_Legacy.fsproj">
        <Project>{0d7b657c-906b-4448-ae64-2153a1fa910c}</Project>
        <Name>FS_PL_Legacy</Name>
    </ProjectReference>
</ItemGroup>

This at least allows VS to reference the F# projects, but those projects are tagged with several warnings and the C# portable library is unable to build as it cannot reference the types in one or both of the F# libraries :(

Most of the warnings in the build output window indicate that the F# libraries cannot be referenced as they appear to have "an indirect dependency on the framework assembly "[System.Threading/System.Lync/etc.]" which could not be resolved in the currently targeted framework"

Upvotes: 15

Views: 1277

Answers (3)

Mackenzie Clark
Mackenzie Clark

Reputation: 63

This bug is old, but still occurs. I ran into this issue when running VS 2015 Community. I realize that this question is about VS 2013, but I was not sure else where to post my answer.

I had a Xamarin Forms Xaml app and I wanted to add an F# PCL to do some calculations. I saw the warning message when trying to reference the F# PCL in my C# PCL:

Unable to add a reference to 'PortableLibrary1'. Portable Library projects can only reference other Portable Library projects and assemblies.

I tried editing the csproj file directly and even tried upgrading to VS 2017 RC but nothing was working.

The fix is a bit of a hack.

  1. First build everything (including the Portable F# project)
  2. Add the F# reference to the C# project by browsing to the output DLL. i.e. Right Click on CS Project, Add -> Reference... -> Browse -> Click "Browse..." button and select the F# project DLL (mine was in my debug folder), then click OK.
  3. The F# PCL now shows in the references list in solution explorer.
  4. Add the F# project reference to the C# project again but by selecting the project i.e. Right Click on CS Project, Add -> Reference... -> Projects -> Check the PortableLibrary project and click OK.
  5. This gives me no error message box about adding the reference, and I can get to all my F# goodness in my C# project.

Upvotes: 2

Rich Turner
Rich Turner

Reputation: 10984

Ask and ye shall be heard ...

I raised this issue on Twitter with Don Syme and some of the F# team. They confirmed that it was an issue they had a fix that was working its way through the release process.

I installed today's Visual F# daily build and I can confirm that this issue has been resolved!

Thanks to Don and the F# team for fixing this issue - I can now incorporate new F# code into my predominantly C# codebase & test suite ... and begin replacing chunks of our system with FAR less F# code than the equivalent C# ;)

Upvotes: 6

kvb
kvb

Reputation: 55184

I don't know the root cause, but you can manually edit the .csproj file to add a reference to the F# project. It will show a warning in the solution explorer indicating that something's still wrong, but it seems to work in practice.

Upvotes: 1

Related Questions