我尝试使用app.config在ServiceLocator中注册类型,例如UIVisualizerService类型。
问题是: ioc.DefaultServiceLocatorConfiguration.Configure(serviceLocator)正常工作,但UIVisualizerService未注册。
App.xaml.cs中的代码出了什么问题?
var serviceLocator = ServiceLocator.Default;
bool canResolveType = serviceLocator.IsTypeRegistered(typeof(IUIVisualizerService));
if (canResolveType)
{
//Expected behaviour
serviceLocator.RemoveAllTypes(typeof(IUIVisualizerService));
canResolveType = serviceLocator.IsTypeRegistered(typeof(IUIVisualizerService));
if (!canResolveType)
{
//Expected behaviour
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var ioc = configuration.GetSection<IoCConfigurationSection>("ioc", "catel");
//<catel>
// <ioc>
// <serviceLocatorConfigurations>
// <serviceLocatorConfiguration>
// <register interfaceType="Catel.Services.IUIVisualizerService"
// implementationType="Catel.Services.UIVisualizerService, Catel.MVVM" />
// </serviceLocatorConfiguration>
// </serviceLocatorConfigurations>
// </ioc>
//</catel>
try
{
ioc.DefaultServiceLocatorConfiguration.Configure(serviceLocator); // to register UIVisualizerService again
}
catch (Exception ex)
{
throw new Exception(String.Format("Error in App.xaml.cs, in serviceLocatorConfiguration : {0}", ex.Message));
// if incorrect implementationType
// ex.Message = "Argument 'serviceImplementationType' cannot be null ..."
}
canResolveType = serviceLocator.IsTypeRegistered(typeof(IUIVisualizerService));
if (!canResolveType)
{
//Unexpected behaviour
Console.WriteLine("Unexpected behaviour"); // It works !!!
}
else
{
//Expected behaviour
// should be here !!!
}
}
}