How to get an instance of a registered service in prism 8? #2670
-
Moved from Prism unity 7.2.0.1422 to 8.1.97. In 7.2.0.1422 I could do this:
Allowed me to initialize the broker with an address once the settings are available. However, now in 8.1.97 there no longer is a TryResolve.
But looks like that gives me a new instance. Because when I use the registered IBroker service afterwards the initialized property is null again. How should I call Init() of GreefaBroker in prism 8? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
This is not a prism question. It actually has nothing to do with Prism. This a Unity Container question. |
Beta Was this translation helpful? Give feedback.
-
So first of all any time you call That said what I would recommend is something like the following: containerRegistry.RegisterSingleton<IBroker>(c =>
{
var broker = c.Resolve<GreefaBroker>();
broker.Init(lSettings.From);
return broker;
}); You do not explicitly need a registration for GreefaBroker as it is a concrete type, this means that by default the container will resolve this as a Transient. Using the delegate / factory extension as shown above you can initialize the instance before returning it for the IBroker registration |
Beta Was this translation helpful? Give feedback.
So first of all any time you call
GetContainer()
you're working with the underlying container (i.e. Unity Container or DryIoc). TryResolve was never part of Prism's API.That said what I would recommend is something like the following:
You do not explicitly need a registration for GreefaBroker as it is a concrete type, this means that by default the container will resolve this as a Transient. Using the delegate / factory extension as shown above you can initialize the instance before returning it for the IBroker registration