Singleton COM comp in c#

  • Thread starter Thread starter GobinathP
  • Start date Start date
G

GobinathP

Hi,

I'm writing a C# Windows Service and I collect some data inside it. Now I
need to expose this to UI clients. I intend to expose a class which the
clients can talk to. The clients can be COM clients. This I'm able to do.

Now if I want to make the c# component a singleton what should I do?

I tried these

1) deriving from ICustomFactory
public MarshalByRefObject CreateInstance(Type serverType)

{

return singletonObject; // where singletonObject is static object of the
class.

}



2)

public class URTServiceProvider : System.ServiceProcess.ServiceBase,
IUrtPlatformManager

{

...............................

public static URTServiceProvider singletonObject;

static void RegisterInner()

{

singletonObject = new URTServiceProvider();

System.Runtime.InteropServices.ExtensibleClassFactory.RegisterObjectCreation
Callback(new
System.Runtime.InteropServices.ObjectCreationDelegate(singletonObject.Activa
te));

}

public IntPtr Activate(IntPtr Aggregator)

{

return (IntPtr)(IUrtPlatformManager)this; //get an error here due to
invalid type casting

}

//This is the static initializer.

static URTServiceProvider()

{

RegisterInner();

}

........................

}

Any Ideas???

Thanks,
Gobi.
 
Singleton is rather simple. Make the constructor private and a private
variable of type of singleton class. You then create a public method to
return the object. Check out the code on www.dofactory.com. Lots of nice
snippets for a variety of patterns.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Thanks for the reply.

I'm looking for Singleton C# objects when accessed by COM clients(Did you
mean to say writing my own class factory component. In that case the
multiple classfactory component should be created by the client). similar to
DECLARE_CLASSFACTORY_SINGLETON() macro in ATL.

Thanks,
Gobi.
 
Back
Top