3
votes

I have a Xamarin Forms project using .NET Standard 2.0 utilizing Autofac for DI.

The project is for Android, iOS and UWP.

When I run the project with iOS or UWP everything works as expected.

When running it in Android i get a "No accessible constructors were found ..." exception when trying to resolve MainViewModel.

Registration: builder.RegisterType<MainViewModel>().AsSelf();

Resolving MainViewModel:

public MainViewModel MainPage
{
    get
    {
        using (var scope = _container.BeginLifetimeScope())
        {
            return _container.Resolve<MainViewModel>();
        }
    }
}

MainViewModel constructor:

public MainViewModel(
    ISecureDatabaseService secureDb, 
    IReceiptQueuedSenderHandler receiptQueuedSenderHandler)

Both ISecureDatabaseService and IReceiptQueuedSenderHandler is registered with Autofac prior to registering MainViewModel.

Any idea what is going on here and why things work smoothly with iOS and UWP and breaks on Android?

Could it be related to Linking in Xamarin Forms?

I'm using Autofac 4.8. I've tried with Autofac 4.6.2 too, but with same results.

2

2 Answers

4
votes

It sounds like it's almost definitely related to Android linking in Forms. I had a nightmare with this until I broke down and created a linker.xml file for each Android app as described here: https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/linker

0
votes

The issue was indeed a Linking issue.

To fix this I followed the guidance in this blog post: Using The Linker In Xamarin Projects.

Specifically to fix this particular issue, I added the Assembly name of the .NET Standard 2.0 shared Xamarin forms project to the Linking section of the Android project:

enter image description here