Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: Basic cross process singleton

$
0
0
I am trying to use MEF with VS2008 .NET 3.5 (Mef_Preview_9) to create a cross process singleton and everything I have tried returns a new instance.

I have created MetaDataModelServer and Client projects, both of which reference a separate IMetaDataModelServer project. When the Client is run multiple times I want them to return the same MetaDataModelServer instance but they always create a new one. My reading says that the default is [PartCreationPolicy(CreationPolicy.Shared)] but even explicitly specifying it, I still get a new instance.

I am feeling a bit stupid about this so please feel free to point out the obvious.
// IMetaDataModel.dll 
   [InheritedExport(typeof(IMetaDataModelServer))]
    public interface IMetaDataModelServer
    {
        DateTime DateTimeCreated { get ;  }
    }

    [InheritedExport(typeof(IMetaDataModel))]
    public interface IMetaDataModel
    {
        DateTime DateTimeCreated { get; }
    }

// MetaDataModel.dll references IMetaDataModel.dll 
    public class MetaDataModelServer : IMetaDataModel.IMetaDataModelServer {}
    public class MetaDataModel : IMetaDataModel.IMetaDataModel {}

// Client.dll references IMetaDataModel.dll
    class Client
    {        
        [Import(typeof(IMetaDataModel.IMetaDataModelServer))]
        public IMetaDataModel.IMetaDataModelServer MetaDataModelServer;

        public Client()
        {
            Compose();
            MessageBox.Show("ClientAddinConsole.MetaDataModelServer " + " " + MetaDataModelServer.DateTimeCreated.ToString() );
        }

        private void Compose()
        {
            dirCatalog = new DirectoryCatalog(@"D:\Projects\MEFExample\MetaDataModel\bin\Debug");
            CompositionContainer container = new CompositionContainer(dirCatalog);
            container.SatisfyImportsOnce(this);
            //container.ComposeParts(this);
     }  
   }

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>