Bug 38930 - DependencyService returns null when .Net Native is enabled on WinRT/WinUAP platform
Summary: DependencyService returns null when .Net Native is enabled on WinRT/WinUAP pl...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: All Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-02-19 18:29 UTC by David Hathaway
Modified: 2016-12-20 19:57 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Repro (167.10 KB, application/zip)
2016-02-19 19:36 UTC, David Hathaway

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description David Hathaway 2016-02-19 18:29:55 UTC
Scenario: When .Net Native is enabled, and the app is compiled in release mode, DependencyService sometimes returns null for DependencyService.Get<T>()

Reason: When WinRT apps are compiled with .Net Native in release, there are no DLL assemblies to iterate and inspect for [assembly: Xamarin.Forms.Dependency (typeof (T))] attributes.  If the [assembly:] attribute is defined in the main executable, it should work, because Forms can iterate over the main executable.  However, if the [assembly:] is defined in a separate DLL, the the WindowsBasePlatformServices.GetAssemblies method doesn't return a full list of assemblies to scan for [assembly:] attributes.

Temporary Workaround:

Use DependencyService.Register<T> to manually register your dependencies
Comment 1 David Hathaway 2016-02-19 19:36:10 UTC
Created attachment 15098 [details]
Comment 2 David Hathaway 2016-02-19 19:43:18 UTC
Steps to reproduce

- Open the attached solution, and set the  dependencyservicerepro.UWP project as your startup project.  
- Select Release | Any CPU (make sure that all your projects are compiled in the build configuration)
- Run the application on your Local Machine


App runs, and the following text is show on the screen of the app.

myFoo is initialized = True
myBar is initialized = False

- Stop the app, and browse to App.xaml.cs in the dependencyservicerepro.UWP project
- Uncomment the following line, and re-run the app in release mode


Result, the following (expected) text is shown:

myFoo is initialized = True
myBar is initialized = True