I want to create an delegate based export for an interface which is called upon every compose call to return every consumer a new instance. But apparently I do get always the same instance back. Is this a limitation of MEF (I am using .NET 4.0)?
Here is the sample code how I tried to create fresh instances for every compose call:
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
namespace TestApp
{
class Program
{
[Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
ITest Instance { get; set; }
static void Main(string[] args)
{
CompositionContainer container = new CompositionContainer();
CompositionBatch batch = new CompositionBatch();
AddExportedValue<ITest>(batch, () =>
{
Console.WriteLine("Create new");
return new Haha();
}, CreationPolicy.NonShared);
container.Compose(batch);
Program p1 = new Program();
container.SatisfyImportsOnce(p1);
// Why do I see only one "Create new" print although CreationPolicy on import and export level is NonShared?
container.SatisfyImportsOnce(p1);
}
interface ITest
{}
class Haha : ITest
{}
static ComposablePart AddExportedValue<iT>(CompositionBatch batch, Func<iT> factory, CreationPolicy policy)
{
string contractNameAndTypeIdentity = AttributedModelServices.GetContractName(typeof(iT));
IDictionary<string, object> metadata = new Dictionary<string, object>();
metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, contractNameAndTypeIdentity);
metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, policy); // <--- the policy seems to be ignored by MEF although incompatible policies are errored out with an exception.
return batch.AddExport(new Export(contractNameAndTypeIdentity, metadata, () => factory()));
}
}
}
Expected Output:
- Create new
- Create new
Actual Output
- Create new
you can use ExportFactory with .NET4.0 too. Glenn Block posted a version a while ago. i use it in my projects too.