I get an error:
An exception has occurred while trying to add a view to region 'MenubarRegion'.
- The most likely causing exception was was:
'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured
while trying to get instance of type MenuView, key "" --->
My MenuView sets its datacontext through MenuViewModel using MEF, which inturn imports an instance of IServiceFactory. I am sure that error is occured due to IServiceFactory and MEF........ I mean Exports on it or Imports. I guess that because when I remove ImportingConstructor and IServiceFactory declarations in MenuViewModel, my program works well.
I have checked for errors on MEF using MefX. Here are the results:
And here is my code:
MenuView.xaml.cs
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MenuView : UserControlViewBase
{
[ImportingConstructor]
public MenuView(MenuViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;
}
}
MenuViewModel.cs
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MenuViewModel : ViewModelBase
{
IServiceFactory _ServiceFactory;
[ImportingConstructor]
public MenuViewModel(IServiceFactory serviceFactory)
{
_ServiceFactory = serviceFactory;
}
protected override void OnViewLoaded()
{
_MenuItems = new ObservableCollection<MenuItem>();
WithClient<IMenuItemService>(_ServiceFactory.CreateClient<IMenuItemService>(), menuItemClient =>
{
MenuItem[] menuItems = menuItemClient.GetAllParentMenuItemsWithChildren();
if (menuItems != null)
{
foreach (MenuItem menuItem in menuItems)
{
_MenuItems.Add(menuItem);
}
_SelectedMenuItem = _MenuItems[2];
}
});
}
private ObservableCollection<MenuItem> _MenuItems;
public ObservableCollection<MenuItem> MenuItems
{
get
{
return _MenuItems;
}
set
{
if (_MenuItems != value)
{
_MenuItems = value;
OnPropertyChanged(() => MenuItems, false);
}
}
}
private MenuItem _SelectedMenuItem;
public MenuItem SelectedMenuItem
{
get
{
return _SelectedMenuItem;
}
set
{
if (_SelectedMenuItem != value)
{
_SelectedMenuItem = value;
OnPropertyChanged(() => SelectedMenuItem);
}
}
}
}
IServiceFactory.cs
public interface IServiceFactory
{
T CreateClient<T>() where T : IServiceContract;
}
ServiceFactory.cs
[Export(typeof(IServiceFactory))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ServiceFactory : IServiceFactory
{
public T CreateClient<T>() where T : IServiceContract
{
return ObjectBase.Container.GetExportedValue<T>();
}
}
Bootstrapper (Client side):
public static class MEFLoader
{
public static CompositionContainer Init()
{
return Init(null);
}
public static CompositionContainer Init(ICollection<ComposablePartCatalog> catalogParts)
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MenuItemClient).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MEFLoader).Assembly));
if (catalogParts != null)
foreach (var part in catalogParts)
catalog.Catalogs.Add(part);
CompositionContainer container = new CompositionContainer(catalog);
return container;
}
}
Bootstrapper (Business side)
public static class MEFLoader
{
public static CompositionContainer Init()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MunimPlusEngine).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MenuItemManager).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MenuItemRepository).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MEFLoader).Assembly));
CompositionContainer container = new CompositionContainer(catalog);
return container;
}
}
Bootstrapper (WPF Main Application)
public class BootStrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleMenu.Module).Assembly));
}
}
App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
ObjectBase.Container = MEFLoader.Init(new List<ComposablePartCatalog>()
{
new AssemblyCatalog(Assembly.GetExecutingAssembly())
});
BootStrapper bootstrapper = new BootStrapper();
bootstrapper.Run();
}
}
Here is my Project if anybody would like to have a look at it:
The problem is that your ServiceFactory
implementation is not added to the MEF catalog. When you add:
public class BootStrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleMenu.Module).Assembly));
//Added catalog
//-->
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ServiceFactory).Assembly));
}
}
to your bootstrapper configuration, the application stops throwing exception on startup. (however it still doesn't show anything)
In order to add that type I needed to add a reference to an additional project in the main application.