0

I have 2 contracts hosted in two servicehosts i need to have a singleton object in the first contract so when the other servicehost try to create an object from that class it just retrieve the same object from the first servicehost,

which might mean i need to find away to access the servicehost and get the object from it instead of creating a new one.. any idea?

thanks in advance

Stacker
  • 8,157
  • 18
  • 73
  • 135
  • Use a Depedency Injection framework like NInject or StructureMap to define a singleton-based implementation for an interface describing your singleton object – marc_s Jul 26 '10 at 14:31
  • i dont think my problem is how to implement singleton , i already did that , the thing is from one endpoint (servicehost) its totaly different process which means when the other endpoint try to create new object it will always find no object because the first object is created in a different process(servicehost) that is why i think i need to find away to access a servicehost from another servicehost – Stacker Jul 26 '10 at 14:38
  • if you have an interface `IMySingleton` and use a DI container and define this as a singleton, then both your service host instances can just simply request a class implementing that interface from the DI container, and the DI container would guarantee that both get the same instance (the singleton instance) – marc_s Jul 26 '10 at 14:52

1 Answers1

0

so, i think you can make the instance of the service class yourself, then pass this instance to both service hosts. I haven't tested this in depth though.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Calc: ICalc
{ 
}

var calc = new Calc();
var h1 = new ServiceHost(calc, baseAddress1);
var h2 = new ServiceHost(calc, baseAddress2);
rocketsarefast
  • 4,072
  • 1
  • 24
  • 18