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();
}
}
Project
Here is my Project if anybody would like to have a look at it:
Download Project
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.
Related
i'm a new in a development of WPF applications with PRISM and AKKA.NET frameworks.
In my code for Shell Window I made an instance of ActorSystem.
public partial class App
{
private ActorSystem appActorSystem;
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
appActorSystem = ActorSystem.Create(System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name);
containerRegistry.RegisterInstance(appActorSystem);
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
moduleCatalog.AddModule<CommandBar.CommandBarModule>();
}
protected override void OnExit(ExitEventArgs e)
{
appActorSystem.Terminate();
appActorSystem.Dispose();
base.OnExit(e);
}
}
The instance will be registered in DI Container (Unity) after creation.
In my application i have also a module.
public class CommandBarModule : IModule
{
private IContainerProvider _containerProvider;
public void OnInitialized(IContainerProvider containerProvider)
{
_containerProvider = containerProvider;
var appActorSystem = _containerProvider.Resolve<ActorSystem>();
var regionManager = _containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion(regionNames.CommandBar, typeof(ViewA));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
}
}
I want to get the instance of the Actorsystem in the module. It works fine in CommandBarModule Class.
But I want also to get the instance of my Actorsystem in the ViewModel of the module...
public class ViewAViewModel : BindableBase
{
private string _message;
public string Message
{
get { return _message; }
set { SetProperty(ref _message, value); }
}
public ViewAViewModel()
{
Message = "test";
}
}
My First Idea was, that i can do injection of IContainerProvider into the constructor of the ViewModel, like this:
public class ViewAViewModel : BindableBase
{
private IContainerProvider _containerProvider;
private string _message;
public string Message
{
get { return _message; }
set { SetProperty(ref _message, value); }
}
public ViewAViewModel(IContainerProvider containerProvider)
{
_containerProvider = containerProvider;
var appActorSystem = _containerProvider.Resolve<ActorSystem>();
Message = "test";
}
}
But it doesn't work...
Could you please explain me how to do it right?
Inject the dependency, not the container:
public ViewAViewModel(ActorSystem appActorSystem)
{
Message = "test";
}
It does not matter in which module a service is registered and where it is resolved, as long as it's resolved after being registered.
This is my UnityResolver Class to create the instance of IUnityContainer
public sealed class UnityResolver
{
private static IUnityContainer _unityContainer;
private static volatile UnityResolver _unityresolverinstance;
private static object syncRoot = new Object();
public static IUnityContainer UnityContainerInitiation
{
get
{
if (_unityContainer == null)
{
if (_unityresolverinstance == null)
{
lock (syncRoot)
{
if (_unityresolverinstance == null)
_unityresolverinstance = new UnityResolver();
}
}
}
return UnityResolver._unityContainer;
}
}
public UnityResolver()
{
_unityContainer = new UnityContainer();
_unityContainer.RegisterType<MaintainRouteViewModel>();
}
}
Below is my Base View and Its ViewModelCode
public partial class MaintainRouteView : UserControl
{
public MaintainRouteViewModel maintainRouteViewModel = null;
IUnityContainer container;
public MaintainRouteView()
{
InitializeComponent();
container = UnityResolver.UnityContainerInitiation;
maintainRouteViewModel = container.Resolve<MaintainRouteViewModel>();
this.DataContext = maintainRouteViewModel;
}
///This button will navigate to the child view.
private void AddRoute_Click(object sender, RoutedEventArgs e)
{
pageAnimationControl.ShowPage(new AddNewRouteView());
}
}
Its ViewModel..
public class MaintainRouteViewModel : viewModelbase
{
private string _statusSuccessMessage = null;
private string _statusFailMessage =null;
private ObservableCollection<RouteDetailsModel> _routeDetailsCollection;
public ObservableCollection<RouteDetailsModel> routeDetailsCollection
{
get
{
return this._routeDetailsCollection;
}
set
{
this._routeDetailsCollection = value;
RaisePropertyChanged("routeDetailsCollection");
}
}
public string StatusSuccessMessage
{
get
{
return _statusSuccessMessage;
}
set
{
_statusSuccessMessage = value;
this.RaisePropertyChanged("StatusSuccessMessage");
}
}
public string StatusFailMessage
{
get { return _statusFailMessage; }
set
{
_statusFailMessage = value;
this.RaisePropertyChanged("StatusFailMessage");
}
}
public MaintainRouteViewModel()
{
///it will load some data to the Observablecollection
getAllCurrentRouteData();
}
}
Now Below is my Child View and its ViewModel....
public partial class AddNewRouteView : UserControl
{
public AddNewRouteView()
{
InitializeComponent();
IUnityContainer container = UnityResolver.UnityContainerInitiation;
this.DataContext = container.Resolve<AddNewRouteViewModel>();
}
}
Its ViewModel....
public class AddNewRouteViewModel : viewModelbase
{
private MaintainRouteViewModel maintainRouteViewModel;
public ICommand SaveCommand
{
get;
set;
}
[InjectionConstructor]
public AddNewRouteViewModel(MaintainRouteViewModel maintainRouteViewModel)
{
this.maintainRouteViewModel = maintainRouteViewModel;
SaveCommand = new DelegateCommand<object>((a) => ValidateNewRoute());
}
private void ValidateNewRoute()
{
bool flag = saveAndValidate();
if(flag)
{
updateRouteStatus();
}
}
public void updateRouteStatus()
{
maintainRouteViewModel.StatusSuccessMessage = "New Route successfully Added..";
}
}
}
Can Anyone Tell me how to use this way to get the same object of MaintainRouteViewModel in my Child VM Constructor So that i will show the Updated Status Message in my Base view MaintainRouteView???
*It will Work Fine If i replace my MaintainRouteView with below code :
this Is an another approach to use IOC .i previously using this in my project. it Works Fine for me but now i want to implement the same thing using Unity Container. Please Help.
public partial class MaintainRouteView : UserControl
{
public MaintainRouteViewModel maintainRouteViewModel = null;
public MaintainRouteView()
{
InitializeComponent();
maintainRouteViewModel = new MaintainRouteViewModel();
this.DataContext = maintainRouteViewModel;
}
private void AddRoute_Click(object sender, RoutedEventArgs e)
{
pageTransitionControl.ShowPage(
new AddNewRouteView
{
DataContext = new AddNewRouteViewModel(maintainRouteViewModel)
});
}
}
I am able to solve this issue using the LifeTime Management of Unity Container Register Types.
it will work fine if i tell the container to create a singleton instance of the MaintainRouteViewModel Class.
using :
container.RegisterType<MaintainRouteViewModel>(
new ContainerControlledLifetimeManager());
But it's just a workaround to get the expected result. i want to achieve it using a proper dependency injection without any singleton instance principle. Can anyone please help to provide the solution.
Starting from the Caliburn Micro "Simple MDI" example. I would like to achieve the following:
I would like to share the reference of a class between the ViewModels. The shareme.count should be passed as reference to all the ViewModels. This would allow me to change it from within each ViewModel.
How can I achieve this close to Caliburn Micro convention?
ShellViewModel.cs
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive {
SharedClass _shareme;
public SharedClass shareme {
get { return _shareme; }
set {
_shareme = value;
NotifyOfPropertyChange(() => shareme);
}
}
public ShellViewModel() {
shareme.count = 1;
}
public void OpenTab() {
ActivateItem(new TabViewModel {
DisplayName = "Tab " + shareme.count++
});
}
TabViewModel.cs
public class TabViewModel : Screen {}
AppBootstrapper.cs
public class AppBootstrapper : BootstrapperBase {
SimpleContainer container;
public AppBootstrapper() {
Initialize();
}
protected override void Configure() {
container = new SimpleContainer();
container.Singleton<IWindowManager, WindowManager>();
container.Singleton<IEventAggregator, EventAggregator>();
container.PerRequest<IShell, ShellViewModel>();
}
protected override object GetInstance(Type service, string key) {
var instance = container.GetInstance(service, key);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service) {
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance) {
container.BuildUp(instance);
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
DisplayRootViewFor<IShell>();
}
}
The will also serve as answer to your duplicate question.
EventAggregator isn't just for "events" you can message pass data to any or all viewmodels that are listening for message or event signature in question.
public class ViewModelA : Screen, IHandle<ShareMeMessageA>
{
private readonly IEventAggregator _events;
private int _sharemecount;
public class ViewModelA(IEventAggregator events){
_events = events;
_events.Subscribe(this);
}
//... other bits out for brevity
//-- EDIT --
public void SomeEventClick(){
_event.PublishOnUiThread(new ShareMeMessageB(){ ... etc ... });
}
protected override void Deactivated(bool close){
_events.Unsubscribe(this);
}
private void Handle(ShareMeMessageA msg)
{
if(msg != null)
sharemecount = msg.Count;
}
}
as this is just an example you don't have to pass the class object at all you can pass any type you want bool, int, float, etc..
I'm about to inject a repository instance into some Web.UI.WebControls.Image derived type:
public class CustomImageControl : Image
{
[Import]
public ICachedNameRepository Repo { get; set; } // Null reference here
private void DynamicImage_PreRender(object sender, EventArgs e)
{
ImageUrl = {some ICachedNameRepository usage}
}
}
Also here is my default page I have implemented for testing purposes:
public partial class _Default : Page
{
[Import]
public ICachedNameRepository Repo { get; set; } // Totally ok here
protected void Page_Load(object sender, EventArgs e)
{
{some ICachedNameRepository usage}
}
}
I have implemented container bootstraping according to official guide with respect of usage Control registering instead of Page:
private void BootStrapContainer()
{
var container = new Container();
container.Options.PropertySelectionBehavior = new ImportAttributePropertySelectionBehavior();
container.Register<ICachedNameRepository, CachedNameRepository>();
container.Register<CustomImageControl>(); // Also I have tried Control and Image types
container.Register<Page>();
var cc = container.GetInstance<CustomImageControl>(); // Correctly instantiated CachedNameRepository instance in Repo field in cc object
container.Verify(); // OK here
Global.Container = container;
}
I left ControlInitializerModule, ImportAttributePropertySelectionBehavior and InitializeHandler routines completely copypasted from guide mentioned earlier
At page loading I ended up with correctly resolved default page instance with CachedNameRepository injected into the right place, but my CustomImageControl suffering from null reference.
This can be done by hooking into the InitComplete event of the Page. This is the code I've used to prove this.
I changed CustomImageControl to inherit from UserControl:
public partial class CustomImageControl : UserControl
{
[Import]
public ICachedNameRepository Repo { get; set; }
private void DynamicImage_PreRender(object sender, EventArgs e)
{
}
}
Here's the updated InitializeHandler
public class Global : HttpApplication
{
private static Container container;
public static void InitializeHandler(IHttpHandler handler)
{
if (handler is Page)
{
Global.InitializePage((Page)handler);
}
}
private static void InitializePage(Page page)
{
container.GetRegistration(page.GetType(), true).Registration
.InitializeInstance(page);
page.InitComplete += delegate { Global.InitializeControl(page); };
}
private static void InitializeControl(Control control)
{
if (control is UserControl)
{
container.GetRegistration(control.GetType(), true).Registration
.InitializeInstance(control);
}
foreach (Control child in control.Controls)
{
Global.InitializeControl(child);
}
}
And the 2 other changes from the documentation. Be sure to call RegisterWebPagesAndControls in your bootstrapper
private static void RegisterWebPagesAndControls(Container container)
{
var pageTypes =
from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>()
where !assembly.IsDynamic
where !assembly.GlobalAssemblyCache
from type in assembly.GetExportedTypes()
where type.IsSubclassOf(typeof(Page)) || type.IsSubclassOf(typeof(UserControl))
where !type.IsAbstract && !type.IsGenericType
select type;
pageTypes.ToList().ForEach(container.Register);
}
class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior
{
public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo)
{
// Makes use of the System.ComponentModel.Composition assembly
return (typeof(Page).IsAssignableFrom(serviceType) ||
typeof(UserControl).IsAssignableFrom(serviceType)) &&
propertyInfo.GetCustomAttributes<ImportAttribute>().Any();
}
}
Im new on Unity/EF world and im getting a Error when i execute my test new project.
I have a Prism bootstrap first:
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return new Shell();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)this.Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
this.Container
.RegisterType<IDataContextAsync, SistemaContext>(new ContainerControlledLifetimeManager())
.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new ContainerControlledLifetimeManager())
.RegisterType<IRepositoryAsync<Cliente>, Repository<Cliente>>()
.RegisterType<IClienteService, ClienteService>();
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(CadastroModule.CadastroModule));
}
}
And other project "CadastroModule" with the CadastroModule:
public class CadastroModule : IModule
{
private readonly IRegionManager regionManager;
public CadastroModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
public void Initialize()
{
this.regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.CadastroCliente));
}
}
Thats my EF Context:
public class SistemaContext : Sistema.Common.Repository.DataContext
{
static SistemaContext()
{//I dont know what to do here!
//Database.SetInitializer<SistemaContext>(new MigrateDatabaseToLatestVersion<SistemaContext, Sistema.DataAccess.Migrations.Configuration>());
}
public SistemaContext()
: base("Name=ContactsDb")
{
}
public DbSet<Cliente> Cliente { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Sistema.DataAccess.Mapping.ClienteConfig());
modelBuilder.Configurations.Add(new Sistema.DataAccess.Mapping.EnderecoConfig());
}
}
I need the "MigrateDatabaseToLatestVersion" for automatic migrations.
The View:
public partial class CadastroCliente : UserControl
{
private readonly IClienteService _clienteService;
private readonly IUnitOfWork _unitOfWork;
public CadastroCliente()
{
InitializeComponent();
}
public CadastroCliente(IClienteService clienteService, IUnitOfWork unitOfWork)
{
_clienteService = clienteService;
_unitOfWork = unitOfWork;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dgv.DataContext = _clienteService.ClienteOrdenadoNome();
}
}
UPDATE 1 IF i take out the IUnitOfWork it runs!:
public partial class CadastroCliente : UserControl
{
private readonly IClienteService _clienteService;
private readonly IUnitOfWork _unitOfWork;
public CadastroCliente()
{
InitializeComponent();
}
public CadastroCliente(IClienteService clienteService)
{
_clienteService = clienteService;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dgv.DataContext = _clienteService.ClienteOrdenadoNome();
}
}
Im using a Generic unit of Work repository located here: Link
And the Error is this:
At the time of the exception, the container was:
Resolving CadastroModule.Views.CadastroCliente,(none)
Resolving parameter \"unitOfWork\" of constructor CadastroModule.Views.CadastroCliente(Sistema.Service.IClienteService clienteService, Sistema.Common.Repository.UoW.IUnitOfWork unitOfWork)
Resolving Sistema.Common.Repository.UoW.IUnitOfWork,(none)
I detected when the UnitOfWork is taking the context
public class UnitOfWork : UnitOfWorkAsync
public UnitOfWork(IDataContextAsync dataContext) { _dataContext = dataContext; }
The dataContext return a internal exception exception:
The context cannot be used while the model is being created.
This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently.
Note that instance members of DbContext and related classes are not guaranteed to be thread safe
What im doing wrong?
Problem resolved after many days...
Internaly of the RepositoryFramework that im using, the EntityClasses need to inherit from a class named Entity:
public abstract class Entity : IObjectState
{
[NotMapped]
public ObjectState ObjectState { get; set; }
}
A child "Complex"(Entity framework Mapping ComplexTypeConfiguration<*>) class, was not inherited.
The error was not the UNIT/PRISM, was the repository itself that im using.
To resolve that i created a new project using no Modularity PRISM to be more simple to find where the error is comming.
So... my problem was resolved by putting the ": Entity" on my Child class:
public class Endereco : Entity //HERE
{
public string Rua { get; set; }
public int Numero { get; set; }
}