I have a Xamarin forms app that is using dryioc for IoC. It seems all my services get disposed as soon as the view is out of scope
This is how I am registering the service in the app.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IInboxService, InboxService>();;
}
and I resolve it this way in the app.cs
protected override void OnInitialized()
{
_started = DateTime.Now;
InitializeComponent();
InitializeAppSettings();
Container.Resolve<ISyncService>().Init(new List<ISyncableService>
{
Container.Resolve<IInboxService>()
});
}
When I need to use it in a viewmodel I put it in the constructor like so.
public class HomeViewModel {
public HomeViewModel(InboxService inboxService)
{
}
}
The singleton is respected but then it will dispose and create a new one when it needs it. Anyone else run into this ?
Xamarin Version: 5.0.0.2125
Prism Version: 8.1.97
LocIOS Version: 8.1.97
public HomeViewModel(InboxService inboxService)
This injects a concrete InboxService
instance, which is independent of the registration of interfaces that it might implement. If you want your singleton, request an IInboxService
:
public HomeViewModel(IInboxService inboxService)
I try to give a service implementing a certain interface a meaningful name that's more than just interface's name minus I
, like DummyInboxService
, TestInboxService
, ImapInboxService
, Pop3InboxService
, RemoteInboxService
... thus making clearer what the respective implementation actually does and helping finding these errors (most likely a typo anyway) early.