1
votes

I have one module with interface IModuleMainService with one event.

e.g. [Export(typeof(IModuleMainService))]

I have SHELL with interface IMainService.

e.g. [Export(typeof(IMainService))]

In ModuleMain I have import [Import(typeof(IMainService)]

In this case application working, but I want to communicate with module and shell.

I try to insert importing to Shell.cs e.g.

[Import]
public IModuleMainService ModuleMainService { get; set; }

But I run application and I get error, see bellow. If I comment [Import] public IModuleMainService... application running succesfull. Where is a problem?

The Point is: Consum event of Module class in SHELL.

System.ComponentModel.Composition Warning: 1 : The ComposablePartDefinition 'Main.Silverlight.Views.Shell' has been rejected. The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "Main.ViewModels.ShellViewModel") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Main.ViewModels.ShellViewModel".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.

Resulting in: Cannot set import 'Main.Silverlight.Views.Shell.ViewModel (ContractName="Main.ViewModels.ShellViewModel")' on part 'Main.Silverlight.Views.Shell'. Element: Main.Silverlight.Views.Shell.ViewModel (ContractName="Main.ViewModels.ShellViewModel") --> Main.Silverlight.Views.Shell --> AssemblyCatalog (Assembly="Main, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

A first chance exception of type 'System.ComponentModel.Composition.ImportCardinalityMismatchException' occurred in System.ComponentModel.Composition

1

1 Answers

2
votes

In prism programming the modules and the shell shall be seperated. No direct reference shall be declared from one module to another or to shell. What you need to do is to employ EventAggregator in order to exchange messages between either modules or shell.

An alternative of using IEventAggregator is to use RegionScopes to pass messages.

As as summary, do not create direct references between modules and shell; use IEventAggregator to exchange messages and declare your message carrying events in a common library which is reused by all of the modules and the shell.