I would like to share my M2MQTT code between a Windows 10 universal windows platform application and a windows phone 8.1 application. Perhaps a WPF app in the future.
I've attempted to use NuGet to include M2MQTT in a portable class library (PCL). I targeted the PCL with a minimum of requirements to satisfy both project types. I receive the following error. Is it possible to reference m2mqtt in a portable class library that can be shared by both UWP and WP8.1 projects?
Attempting to gather dependencies information for package 'M2Mqtt.4.2.0.1' with respect to project 'Pcl45', targeting '.NETPortable,Version=v4.6,Profile=Profile32'
Attempting to resolve dependencies for package 'M2Mqtt.4.2.0.1' with DependencyBehavior 'Lowest'
Resolving actions to install package 'M2Mqtt.4.2.0.1'
Resolved actions to install package 'M2Mqtt.4.2.0.1'
Install failed. Rolling back...
Package 'M2Mqtt 4.2.0.1' does not exist in project 'Pcl45'
Could not install package 'M2Mqtt 4.2.0.1'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.6,Profile=Profile32', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
Additional Information 9/24/2015
I cloned the M2MQTT GitHub repository and noticed that there is a .pcl for Windows 8.1 and Windows phone 8.1. I didn't see support for UWP in a .pcl though.
I suppose I have my answer. No M2MQTT does not support .pcl in UWP projects.
Wondering if there are any plans to add .pcl support in the future?
Also if I might be able to use M2MQTT in Xamarin Android or Ios projects?
Additional Information 9/28/2015 Following suggestions from @Anders Gustafsson I successfully created a .pcl that I was able to consume in my UWP app and windows phone 8.1 app. No more duplication of code. Nice. Hope this will work in xamarin android and ios.
There is this warning.
Severity Code Description Project File Line Source Warning
Some NuGet packages were installed using a target framework different from
the current target framework and may need to be reinstalled. Visit
http://docs.nuget.org/docs/workflows/reinstalling-packages for more
information. Packages affected: M2Mqtt Pcl45 0 Build
M2MQTT is available as a PCL profile 32 assembly, which is capable of targeting Windows 8.1, Windows Phone 8.1 and Windows 10/UWP projects. The M2Mqtt.WinRT assembly is really a PCL Profile 32 assembly.
The problem you are facing is that this fact is not reflected in the NuGet package. In the NuGet package there are separate lib folders for win81 and wpa81 with the M2Mqtt.WinRT assembly in both. The desirable scenario would have been that there should have been a single sub-folder portable-win81+wpa81 containing the M2Mqtt.WinRT, then everything would (probably) have worked flawlessly for you.
There seems to be one workaround, although I cannot promise that it is fully reliable.
Now, you should be able to consume the M2Mqtt.WinRT assembly from your PCL, and you should also be able to reference your PCL in a UWP application.
To add more reliability once you have managed to download the M2MQTT package from NuGet, you might manually delete and then re-add the reference to the M2Mqtt.WinRT assembly in your PCL project. That way you will short-circuit NuGet, preventing NuGet from accidentally trying to restore your M2Mqtt.WinRT reference from a non-existing (portable-win81+wpa81) directory.