I've got a project where contains two modules, one infrastructure (Common
library) and the Shell
.
Note that Common
has a FooService
, this one has an ExportAttribute
[Export]
public class FooService
{
}
And this one should be used by Module1
and Module2
, But it throws me errors if I have the ImportAttribute
. Please note the comment.
[ModuleExport("Module1.ModuleInit", typeof(Module1.ModuleInit))]
public class ModuleInit : IModule
{
private readonly IRegionManager _regionManager;
public IServiceLocator _serviceLocator;
// [Import(AllowRecomposition=true)]
public FooService _service;
[ImportingConstructor]
public ModuleInit(IRegionManager regionManager, IServiceLocator serviceLocator)
{
_regionManager = regionManager;
_serviceLocator = serviceLocator;
}
public void Initialize() { }
}
This code is the same for Module2
.
An exception occurred while initializing module 'Module2.ModuleInit'. - The exception message was: 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) More than one export was found that matches the constraint '((exportDefinition.ContractName == "Common.FooService") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Common.FooService".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'.
Resulting in: Cannot set import 'Module1.ModuleInit._service (ContractName="Common.FooService")' on part 'Module1.ModuleInit'. Element: Module1.ModuleInit._service (ContractName="Common.FooService") --> Module1.ModuleInit --> AssemblyCatalog (Assembly="Module1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Why am I receiving this exception? I'm just exporting one object. I'd like to know what is happening and how to fix it.
Please feel free to download it, it's very small the project. Download the compact project