// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.ServiceModel; using WCF.Common; namespace WCF.Server { [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple, UseSynchronizationContext=false)] public class Test1 : ITest { private int counter; public int Add(int a, int b) { return a + b; } public String Dup(String s) { return s + s; } public Data Process(Data d) { return new Data(d.Iv + 1, d.Sv + "X"); } public int GetCounter() { counter++; return counter; } public void Noop() { // nothing } } [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall, ConcurrencyMode=ConcurrencyMode.Single)] public class Test2 : ITest { private int counter; public int Add(int a, int b) { return a + b; } public String Dup(String s) { return s + s; } public Data Process(Data d) { return new Data(d.Iv + 1, d.Sv + "X"); } public int GetCounter() { counter++; return counter; } public void Noop() { // nothing } } public class Program { private const int SERVER_PORT = 12345; private const string NAME1 = "Test1"; private const string NAME2 = "Test2"; public static void Main(string[] args) { ServiceHost host1 = new ServiceHost(typeof(Test1)); host1.AddServiceEndpoint(typeof(ITest), new NetTcpBinding(), string.Format("net.tcp://localhost:{0}/{1}", SERVER_PORT, NAME1)); host1.Open(); ServiceHost host2 = new ServiceHost(typeof(Test2)); host2.AddServiceEndpoint(typeof(ITest), new NetTcpBinding(), string.Format("net.tcp://localhost:{0}/{1}", SERVER_PORT, NAME2)); host2.Open(); Console.ReadKey(); } } }