The following service will allow you to check how Indigo instancing behaviour works:
using System;using System.ServiceModel;
[ServiceContract]interface ICanRandom{ [OperationContract] int Randomize(int intLow, int intHigh);
[OperationContract] int GetLastNumber();}
[ServiceBehavior(InstanceMode = InstanceMode.Singleton, ConcurrencyMode = ConcurrencyMode.Multiple)]public class RandomizerService : ICanRandom{ private int intLastNumber; private Random r = new Random(DateTime.Now.Millisecond);
public int Randomize(int intLow, int intHigh) { System.Threading.Thread.Sleep(5000); int intRandomNumber = r.Next(intLow, intHigh); intLastNumber = intRandomNumber; return intRandomNumber; }
public int GetLastNumber() { return intLastNumber; }}
This service will return a random number for multiple clients. Try simulating it with the following client code:
using System;using System.Threading;
class Program{ static void Main(string[] args) { Thread t1 = new Thread(new ParameterizedThreadStart(Program.CallService)); Thread t2 = new Thread(new ParameterizedThreadStart(Program.CallService)); Console.WriteLine("Threads running."); t1.Start(1); t2.Start(2); }
static void CallService(object intThread) { CanRandomProxy objWS = new CanRandomProxy("ICanRandom"); Console.WriteLine("Thread {0} - Method call.", intThread); Console.WriteLine("Thread {0} - Random number: {1}", intThread, objWS.Randomize(10, 110)); Console.WriteLine("Thread {0} - Last random number: {1}", intThread, objWS.GetLastNumber()); Console.WriteLine("Thread {0} - Done.", intThread); }}
Singleton services process all requests using a single service instance. Try changing the ConcurrencyMode property of ServiceBehaviour attribute to obtain different instancing semantics. Having InstanceMode set to PerCall (current default) will disable ConcurrencyMode.Multiple behaviour, which is expected.