Indigo - Instancing 

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.

Categories:  .NET 3.0 - WCF
Sunday, 27 March 2005 21:04:33 (Central Europe Standard Time, UTC+01:00)  #    Comments

 

Copyright © 2003-2024 , Matevž Gačnik
Recent Posts
RD / MVP
Feeds
RSS: Atom:
Archives
Categories
Blogroll
Legal

The opinions expressed herein are my own personal opinions and do not represent my company's view in any way.

My views often change.

This blog is just a collection of bytes.

Copyright © 2003-2024
Matevž Gačnik

Send mail to the author(s) E-mail