Search code examples
c#.netnugetnuget-packageportable-class-library

Create Portable Class Library that targets all platforms (and publishing to nuget)


I have created the library Staty that I want to publish via nuget for other developers to use.

I explicitly created it as Portable Class Library and restricted myself to very basic programming constructs. Now I want to publish it in a way, that others can easily use my library. I've set up my library as PCL with the following targets, which seemed to me as the most permissive platform-combination:

Targets of my PCL library

After completing the first version, I wanted to publish it, using Nuget GUI with the following nuget profile. I included the release-build files into the package under the portable-net4-sl4-wp7-win8 folder (I've tried portable-net45-sl5-wp8-win8 too, without any effect) NuGet Profile

To verify, that my library is truly portable, I wanted to include it into another PCL-project, but unfortunately I'm only getting the following error: Could not install package 'Staty 1.0.0'. You are trying to install this package into a project that targets '.NETPortable, Version=v4.5, Profile=Profile259', but the package does not contain any assembly references or content files that are compatible with that framework.

Error when including package

When adding it to a regular console-application, everything works fine. It also works fine, when directly including the dll into that very same PCL-project via References -> Add Reference ... -> Browse.

So what is wrong with the way I published the package?

Additionally I tried adding folder dotnet and net45 to the nuget package with the same dlls - without any success.


Solution

  • In the Change Targets dialog, you are specifying .NET Framework 4.5, whereas in your NuGet setup you are targeting net4. Moreover, in the NuGet setup you are also targeting sl4 and wp7. All in all, this means that when the NuGet Manager tries to match the library path in your NuGet package with the profile of your other PCL project, it is bound to fail due to incompatibilities.

    Please take a look at this list to determine in which lib sub-folder you should place your Staty.dll PCL. (The list does not explicitly specify ASP.NET Core and Xamarin targets, they are normally implicitly accounted for.) From the list you can see that the target platforms you selected in the Change Targets dialog matches Profile 259, and the NuGet target for this profile should preferably be portable-net45+netcore45+wpa81+wp8.

    (If you open the Staty.csproj project file in a text editor, you can confirm that the <targetFrameworkProfile> is indeed Profile259.)

    Please change the lib sub-folder name in your NuGet file to portable-net45+netcore45+wpa81+wp8, and you should then be able to reference the Staty NuGet package from your other PCL project as long this other project targets the same or a sub-set of the platforms that the Staty NuGet package is targeting.