getting error 'Microsoft.Practices.ServiceLocation.ActivationException' occurred in Microsoft.Practices.ServiceLocation.dll

夙愿已清 提交于 2019-12-04 19:07:14

I see you solved your problem by adding the EventAggregator to your container.

However, I would suggest that your ViewModel should not be using the ServiceLocator to resolve the EventAggregator at all. The ServiceLocator is a glorified static instance that is basically an anti-pattern (see Service Locator is an Anti-Pattern). Your ViewModel should most likely accept the EventAggregator in the constructor via dependency injection and use it from there. I don't think you should need a container to test your ViewModels. You could just construct them with their constructors and pass them any mock implementations of their dependent objects (as parameters to the constructor).

GOstrowsky

Based on my understanding, the Service Locator gets initialized when running and initializing the Bootstrapper. Therefore, the exception you are issuing would be caused by not having the Locator initialized.

I believe the following post would be useful considering the non-Prism alternative taking into account that you didn't run the Bootstrapper on the Unit Test scope:

You would need to set the EventAggregator in the Mock Container, and then set the Service Locator service for that container:

(Quoted from above link)

IUnityContainer container = new UnityContainer();

// register the singleton of your event aggregator

container.RegisterType( new ContainerControlledLifetimeManager() );

ServiceLocator.SetLocatorProvider( () => container );

I hope this helped you,

Regards.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!