Skip to content

Singleton Hosting Overview

tylerje edited this page Oct 23, 2014 · 1 revision

When using ServiceWire, all hosted implementations are treated as a singleton. Each request is processed on a different thread.

Warning: Your implementation MUST be thread safe. If it is not, you will experience unpredictable behavior.

With that said, ServiceWire supports using that same singleton instance of your implementation on more than one hosted end point and more than one protocol. For example, in the code below, three different service implementations are constructed. Two of them are hosted on both Named Pipes and TCP/IP. The third is only hosted on the TCP/IP host:

 static void Main(string[] args)
 {
    var logger = new Logger(logLevel: LogLevel.Debug);
    var stats = new Stats();
 
    var ip = ConfigurationManager.AppSettings["ip"];
    var port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
    var ipEndpoint = new IPEndPoint(IPAddress.Any, port);
 
    var pipeName = "ServiceWireTestHost";
    
    var tester = new NetTester();
    var mytester = new MyTester();
    var valTypes = new ValTypes();
 
    var nphost = new NpHost(pipeName, logger, stats);
    nphost.AddService<INetTester>(tester);
    nphost.AddService<IMyTester>(mytester);
 
    nphost.Open();
 
    var tcphost = new TcpHost(ipEndpoint, logger, stats);
    tcphost.AddService<INetTester>(tester);
    tcphost.AddService<IMyTester>(mytester);
    tcphost.AddService<IValTypes>(valTypes);
 
    tcphost.Open();
 
    Console.WriteLine("Press Enter to stop the dual host test.");
    Console.ReadLine();
 
    nphost.Close();
    tcphost.Close();
 
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
 }
Clone this wiki locally