Reputation: 1513
My app has a background service that may be run independently of the main app (HostApduService).
How can I access/use Prism's IContainer from this background service?
I am aware that IContainer is available from Xamarin.Forms.Application.Current, see:
However it is possible in my case that the App doesn't exist, if the service is run without bringing up the app itself.
Upvotes: 4
Views: 874
Reputation: 5799
There are a couple options for you if App.Current
is null.
1) in your background service initialize a new instance of the App:
var app = new App();
app.Container.Resolve<IMyService>();
2) use a helper method that only registers what you want and create the container you need for your background service:
public class App : PrismApplication
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
RegisterBackgroundServices(containerRegistry);
}
public static void RegisterBackgroundServices(IContainerRegistry containerRegistry)
{
// Register services you need in the Background Service
}
}
public class MyBackgroundService
{
IContainerExtension Container { get; }
public MyBackgroundService()
{
// Note: Prism has some non-default Rules in place
Container = new DryIocContainerExtension(new Container());
App.RegisterBackgroundServices(Container);
}
}
Upvotes: 5