Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to export parts from an object not instantiated by the MEF container
    text
    copied!<h1>Introduction</h1> <p>Class <code>SessionModel</code> is a service locator providing several services (I am going to elaborate my system architecture in the future, but for now I need to do it that way).</p> <h1>Code</h1> <p>I edited the following code part to be a Short, Self Contained, Correct (Compilable), Example (SSCCE):</p> <pre class="lang-c# prettyprint-override"><code>using System; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; namespace ConsoleApplication1 { internal class Program { private static void Main(string[] args) { var sessionModel = new SessionModel(3); // first case (see text down below): var compositionContainer = new CompositionContainer(); // second case (see text down below): //var typeCatalog = new TypeCatalog(typeof (SessionModel)); //var compositionContainer = new CompositionContainer(typeCatalog); compositionContainer.ComposeExportedValue(sessionModel); var someService = compositionContainer.GetExportedValue&lt;ISomeService&gt;(); someService.DoSomething(); } } public class SessionModel { private int AValue { get; set; } [Export] public ISomeService SomeService { get; private set; } public SessionModel(int aValue) { AValue = aValue; // of course, there is much more to do here in reality: SomeService = new SomeService(); } } public interface ISomeService { void DoSomething(); } public class SomeService : ISomeService { public void DoSomething() { Console.WriteLine("DoSomething called"); } } } </code></pre> <h1>Problem</h1> <p>I would like MEF to consider the parts (i.e. <code>SomeService</code>) exported by the service locator when composing other parts, but unfortunately this does not work.</p> <h2>First Case</h2> <p>When I try to get the exported value for <code>ISomeService</code> there is a <code>System.ComponentModel.Composition.ImportCardinalityMismatchException</code> telling me there are no exports with this contract name and required type identity (<code>ConsoleApplication1.ISomeService</code>).</p> <h2>Second Case</h2> <p>If I create the <code>CompositionContainer</code> using the <code>TypeCatalog</code> the exception is slightly different. It is a <code>System.ComponentModel.Composition.CompositionException</code> telling me MEF doesn't find a way to create a <code>ConsoleApplication1.SessionModel</code> (which is right and the reason why I am doing it myself).</p> <h1>Additional Information</h1> <p><code>mefx</code> says for both cases:</p> <pre><code>[Part] ConsoleApplication1.SessionModel from: DirectoryCatalog (Path=".") [Export] ConsoleApplication1.SessionModel.SomeService (ContractName="ConsoleApplication1.ISomeService") [Part] ConsoleApplication1.SessionModel from: AssemblyCatalog (Assembly="ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") [Export] ConsoleApplication1.SessionModel.SomeService (ContractName="ConsoleApplication1.ISomeService") </code></pre> <p>What do I have to do? Is this possible with MEF or do I have to use Unity or StructureMap, or something else? Can this be done implementing an <code>ExportProvider</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload