locked
MEF How to create a cross process singleton RRS feed

  • Question

  • I have a client application that I want to share a common instance between invocations but I everything that I have tried end up with a new instance for each time it is run.

    I have simplified my problem as follows with the 3 projeccts in the one solution:

    // ClassLibrary: IServer.dll
    namespace IServer
    {
        public interface IServer
        {
            DateTime DateTimeCreated { get; }
        }
    }
    
    // ClassLibrary: Server.dll references IServer.dll
    namespace Server
    {
        [Export(typeof(IServer.IServer))]
        public class Server : IServer.IServer
        {
            public static DateTime _dateTimeCreated = DateTime.Now;
            public DateTime DateTimeCreated { get { return _dateTimeCreated; } }
        }
    }
    
    // ConsoleApplication: Client.dll references IServer.dll
    
       class Program
        {
            [Import(typeof(IServer.IServer))]
            public IServer.IServer _server;
            
            static void Main(string[] args)
            {
                Program p = new Program();
                p.Run();
            }
    
            void Run()
            {
                Compose();
                Console.WriteLine("Client.Server " + _server.DateTimeCreated.ToString());
                Console.ReadKey();
            }
            private void Compose()
            {
                CompositionContainer container = new CompositionContainer(
                    new DirectoryCatalog(@"D:\Projects\MEFExample 2\Server\bin\Debug"));
                container.ComposeParts(this);
            }
        }

    When I run Client.exe multiple times I get separate (not shared) Server _server objects. I want the same (singleton) instances shared.


    Mark Latham

    Monday, February 18, 2013 3:24 AM

Answers

  • Hi Mark,

    Thank you for your clarify.

    Now, we may need to call it as process communication, or interprocess communication, rather than cross-process singleton.

    And here is an article about IPC(interprocess communication): http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx 

    The Windows operating system provides mechanisms for facilitating communications and data sharing between applications. Collectively, the activities enabled by these mechanisms are called interprocess communications (IPC). Some forms of IPC facilitate the division of labor among several specialized processes. Other forms of IPC facilitate the division of labor among computers on a network.

    And here is a code sample about IPC: http://1code.codeplex.com/wikipage?title=IRPC

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Leisvan Cordero Friday, March 1, 2013 6:57 AM
    • Marked as answer by Mike Feng Tuesday, March 5, 2013 1:03 PM
    Thursday, February 28, 2013 9:11 AM

All replies

  • Hi Mark,

    Welcome to the MSDN Forum.

    Based on my understanding, it seems that you need to know how to implement singleton, if so, please refer to this documentation: http://msdn.microsoft.com/en-us/library/ff650849.aspx

    Singleton provides a global, single instance by:

    • Making the class create a single instance of itself.
    • Allowing other objects to access this instance through a class method that returns a reference to the instance. A class method is globally accessible.
    • Declaring the class constructor as private so that no other object can create a new instance.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Robin_Ren Tuesday, February 26, 2013 3:44 PM fix post
    Tuesday, February 19, 2013 2:11 PM
  • Mike, I had to reply to myself as the Reply to you link was not working.

    Please also note that I am using VS2008 with Mef_Preview_9. If this is a problem I may be able to upgrade.

    I believe that the pattern you have identified will only provide a Singleton within an AppDomain. I need a Singleton that is not only cross AppDomain but cross process. As per my example, when I launch Client.exe twice, I want to get the same instance. I thought that MEF should be able to do this for me. The only thing that I seen that may be the answer is a [Shared] attribute https://mef.codeplex.com/wikipage?title=Sharing%20and%20lifetime but I could not find the attribute in the ComponentModel. I feel like I am missing something basic here.

    If MEF can't do it for me the other possiblities that I see are Remoting  and serialising with file watchers for communication. Neither of which sound that great to me.


    Mark Latham

    Tuesday, February 19, 2013 3:01 PM
  • Hi Mark,

    >>If MEF can't do it for me the other possiblities that I see are Remoting  and serialising with file watchers for communication. Neither of which sound that great to me.

    This question can be better answered there: http://social.msdn.microsoft.com/Forums/en-US/wcf/threads 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Yubo. Zhang Monday, February 25, 2013 9:28 AM
    • Unmarked as answer by Mike Feng Monday, February 25, 2013 10:37 AM
    Saturday, February 23, 2013 2:38 PM
  • It is very helpful.


    Yubo Zhang
    Application Support
    EPX Service Enginerring Tier1

    Monday, February 25, 2013 9:38 AM
  • Marked as answered but it wan't really but I assume that I was in the wrong ballbark with MEF. The answer for me looks like WCF.

    Mark Latham

    • Proposed as answer by Kelmen Wednesday, February 27, 2013 7:10 AM
    • Unproposed as answer by Mike Feng Wednesday, February 27, 2013 7:36 AM
    Monday, February 25, 2013 9:52 AM
  • Hi Mark,

    >>The only thing that I seen that may be the answer is a [Shared] attribute https://mef.codeplex.com/wikipage?title=Sharing%20and%20lifetime but I could not find the attribute in the ComponentModel. I feel like I am missing something basic here.

    Please try this way: http://msdn.microsoft.com/en-us/magazine/ee291628.aspx

    [partCreationPolicy(CreationPolicy.Shared)]
    
    [Export(typeof(ISalesOrderView))]
    
    public partial class SalesOrderView : UserControl, ISalesOrdderView
    
    {
    
    public SalesOrderView()
    
      {
    
      }
    
    }
      

    Here is the MSDN document: http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.partcreationpolicyattribute.creationpolicy.aspx

    And here is the enumerate: http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.creationpolicy.aspx  

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 27, 2013 2:50 AM
  • Thanks Mike but once again I believe this will only provide a cross-Appdomain singleton and not a cross-process singleton. My trials with WCF are providing the cross-process singleton that I need but boy, what a struggle!

    Mark Latham

    Wednesday, February 27, 2013 1:37 PM
  • Hi Mark,

    Thank you for your clarify.

    Now, we may need to call it as process communication, or interprocess communication, rather than cross-process singleton.

    And here is an article about IPC(interprocess communication): http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx 

    The Windows operating system provides mechanisms for facilitating communications and data sharing between applications. Collectively, the activities enabled by these mechanisms are called interprocess communications (IPC). Some forms of IPC facilitate the division of labor among several specialized processes. Other forms of IPC facilitate the division of labor among computers on a network.

    And here is a code sample about IPC: http://1code.codeplex.com/wikipage?title=IRPC

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Leisvan Cordero Friday, March 1, 2013 6:57 AM
    • Marked as answer by Mike Feng Tuesday, March 5, 2013 1:03 PM
    Thursday, February 28, 2013 9:11 AM