1
votes

I'm trying to add DryIoc adapter for MS DI to Xamarin.Forms shared library (.net standard class library) but I couldn't yet.

When I run the project I get this error

05-20 11:49:29.528 D/ ( 3402): HostConnection::get() New Host Connection established 0xda77ac90, tid 3402 05-20 11:49:29.533 D/Atlas ( 3402): Validating map... Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

05-20 11:50:18.777 D/Mono ( 3402): DllImport attempting to load: '/system/lib/liblog.so'. 05-20 11:50:18.777 D/Mono ( 3402): DllImport loaded library '/system/lib/liblog.so'. 05-20 11:50:18.777 D/Mono ( 3402): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so'). 05-20 11:50:18.777 D/Mono ( 3402): Searching for '__android_log_print'. 05-20 11:50:18.777 D/Mono ( 3402): Probing '__android_log_print'. 05-20 11:50:18.777 D/Mono ( 3402): Found as '__android_log_print'. Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

05-20 11:50:18.790 I/MonoDroid( 3402): UNHANDLED EXCEPTION: 05-20 11:50:18.791 I/MonoDroid( 3402): System.NullReferenceException: Object reference not set to an instance of an object. 05-20 11:50:18.791 I/MonoDroid( 3402): at Prism.Common.PageUtilities.GetCurrentPage (Xamarin.Forms.Page mainPage) [0x00002] in D:\a\1\s\Source\Xamarin\Prism.Forms\Common\PageUtilities.cs:212 05-20 11:50:18.791 I/MonoDroid( 3402): at Prism.PrismApplicationBase.OnSleep () [0x00000] in D:\a\1\s\Source\Xamarin\Prism.Forms\PrismApplicationBase.cs:193 05-20 11:50:18.791 I/MonoDroid( 3402): at Xamarin.Forms.Application.SendSleep () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Application.cs:297 05-20 11:50:18.791 I/MonoDroid( 3402): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnStateChanged () [0x00057] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:345 05-20 11:50:18.791 I/MonoDroid( 3402): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnStop () [0x00019] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:293 05-20 11:50:18.791 I/MonoDroid( 3402): at Android.App.Activity.n_OnStop (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <788a34f7a7b84486905dfde786529d42>:0 05-20 11:50:18.791 I/MonoDroid( 3402): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.15(intptr,intptr) 05-20 11:50:18.806 W/art ( 3402): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable

If I remove IUserSocialService from MainPageViewModel's constructor works fine. I don't know why not it's injecting the implementation of IUserSocialService.

ServiceLayer

public static IServiceCollection Init()
{
    var serviceCollection = new ServiceCollection();

    serviceCollection.AddSingleton<IUserSocialService, UserSocialService>();
    return serviceCollection;
}

Xamarin.Forms (Shared net standard class library)

protected override IContainerExtension CreateContainerExtension()
{
    var containerExtension = (DryIocContainerExtension)base.CreateContainerExtension();
    containerExtension.Instance.Populate(ServicesLayer.Init());
    return containerExtension; 
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation<NavigationPage>();
    containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
}

MainPageViewModel

private readonly IUserSocialService userSocialService;

public MainPageViewModel(INavigationService navigationService, IUserSocialService userSocialService) : base(navigationService)
{
    Title = "Main Page";
    this.userSocialService = userSocialService;
}

Packages version:

Service Layer:

<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />

Xamarin.Forms (shared net standard class library)

<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="2.1.0" />
<PackageReference Include="Prism.DryIoc.Forms" Version="7.1.0.431" />

Thanks in advance!

1
NRE message is not useful without stack trace. Could you add it?dadhi
@dadhi I edit my answerMauro Petrini

1 Answers

1
votes

By default Prism does not support Microsoft DI. However if this is a need you have you may want to try out the Prism.Container.Extensions. That project has several helpers available including the ability to handle working with IServiceProvider and IServiceCollection along with a package to make working with Shiny which uses Microsoft DI completely seamless. You can find the out more on the how to on GitHub.