Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS StackOverflow exception

This is yet another variation of the same question where a StackOverflow exception occurs as a result of the 256K stack size while running under IIS. This issue is nothing new and it has been asked several times (here and here)

My question is a little different. The exception is thrown when a client requests data and the WCF service running under IIS 7 tries to serialize a rather large object graph. It actually occurs during the serialization

I can easily reproduce the issue in the development environment by running a retrieve/serialize routine in a thread with a limited stack size:

static void Main(string[] args)
{
   Thread t = new Thread(DoWork, 262144);
   t.Start();
   t.Join();
   Console.ReadLine();
}

private static void DoWork()
{
   var dataAccess = new DataAccess();

   var data = dataAccess.LoadData();

   var serializer = new DataContractSerializer(typeof(List<Data>), null, int.MaxValue, false, true, new DataContractSurrogate());

   var memoryStream = new MemoryStream();
   serializer.WriteObject(memoryStream, data );
}

This simulates the StackOverflow exception just like in IIS. When I change the stackSize parameter passed to the Thread's constructor to 1MB, it works fine...

My question is how can one do this inside of a WCF service method? In other words, in my WCF service method I don't explicitly create a serializer and call WriteObject. How/where can I do this same sort of work around in a thread where I can control the stackSize?

Thanks!

like image 415
John Russell Avatar asked Jun 19 '12 21:06

John Russell


1 Answers

You can modify the default stack size by changing the PE header of the executable. Use editbin.exe with the /stack argument. See http://msdn.microsoft.com/en-us/library/35yc2tc3(v=vs.80).aspx

like image 145
Brian Rasmussen Avatar answered Sep 29 '22 11:09

Brian Rasmussen