cjlotz / Xamarin.Plugins

Cross platform Xamarin Plugins
MIT License
113 stars 56 forks source link

Incorrect assembly for Android in NuGet package v3.2.1? #36

Closed eloekset closed 8 years ago

eloekset commented 8 years ago

Either the Android assembly in v3.2.1 of the NuGet package is mixed up with the PCL assembly, or I must be misunderstanding something. See the screenshot from a debug session running on my Android device, showing the EmailMessenger implementation in my Android project throwing NotImplementedException. Looking through the code, it seems like this exception should be thrown in the PCL implementation, but not in the Android implementation. xam plugins messaging notimplementedexception

cjlotz commented 8 years ago

@eloekset I'll run some tests on my side this evening. Get back to you soon.

cjlotz commented 8 years ago

@eloekset Done some investigation. The plugin uses delayed initialization to initialize the CrossMessaging.Current singleton that the MessagingPlugin class uses behind the scenes. I've never been a fan of the singleton approach but this is the design recommended for providing consistent API across the different Xamarin plugins.

The singleton gets initialized on first access. If first access happens from a PCL assembly where the platform specific functionality is not supported, you may end up with a singleton initialized incorrectly. When you subsequently access the API from a non-PCL assembly you will then get the exception.

Can you perhaps verify whether you are accessing the plugin functionality from within a PCL before using it in the Android library snippet above?

eloekset commented 8 years ago

Thanks for your help @cjlotz! Before closing this issue, I'll post an update on what I found, although I'm not really sure what really caused the problem.

  1. Since I'm wrapping the CrossMessaging code inside my own SendEmailService, I created a default constructor in the Android implementation of that service to make sure I use the CrossMessaging.Current.EmailMessenger before the XamarinForms Portable library touches it: 2016-07-09_14-06-12
  2. Since I still got the same issue after doing step 1, I decided to create a sample project using XamarinForms Shared instead of Portable to test the Xam.Plugins.Messaging NuGet package when installed in a Shared project. That sample worked perfectly fine. So I created another sample project using XamarinForms Portable and did exactly the same as in my real project (SendEmailService, access the EmailMessenger from the constructor etc...) and expected the exception to be thrown - but it didn't!
  3. I examined properties of my XamarinForms Portable sample project that worked fine, and compared them to my real project. I found that my real project used another Portable profile including Windows Phone Silverlight 8 instead of Windows Phone 8.1, which my sample project used. So I changed my real project to use the same profile, which prompted me to uninstall all NuGet packages first, because Visual Studio couldn't automatically update the project to use NuGet 3.0, which was required.... After doing all that, I reinstalled the packages, rebuilt and tested, and the problem was still there.
  4. Since I couldn't find any difference between my real project and my sample project, I uninstalled Xamarin.Forms and Xam.Plugins.Messaging NuGet packages from all my projects, closed Visual Studio, deleted the packages folder and all bin and obj folders for all projects, opened Visual Studio and installed the same NuGet packages again, built, tested, and now it worked!