This project has moved. For the latest updates, please go here.

StackOverflowException when saving a package ...

Sep 23, 2012 at 10:45 AM


I developed a simple method to create a spreadsheet from a database view:

        public static byte[] CreateXlsFile(Type xlsReportType, IRelationPredicateBucket predicate, int maxNumberOfTuplesToReturn, ISortExpression sortExpression)
            IEnumerable<string> fieldNames;
            if (!s_EntityToXlsMappings.TryGetValue(xlsReportType, out fieldNames))
                throw new ApplicationException(String.Format("Export of type {0} not supported.", xlsReportType.Name));

            using (ExcelPackage package = new ExcelPackage())
                // Add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Data");
                if (worksheet != null)
                    ExcelRangeBase range = worksheet
                        .Cells[1, 1]
                return package.GetAsByteArray();

        public static byte[] CreateXlsFile<T>(IRelationPredicateBucket predicate, int maxNumberOfTuplesToReturn, ISortExpression sortExpression)
            where T : CommonEntityBase
            return CreateXlsFile(typeof(T), predicate, maxNumberOfTuplesToReturn, sortExpression);

All went well in my tests using following code, I could generate sheets with more then 50000 rows:

        public void MyTestInitialize()
            GlobalService = new GlobalService();

        [Ignore(@"ExportToXls integration test")]
        public void ExportToXls()
            IRelationPredicateBucket filterBucket = null;// new RelationPredicateBucket(TobePaidOverviewFields.PaymentTypeId == (int)PaymentStateEntity.Values.SENDFTCS);
            byte[] spreadSheet = GlobalService.ExportToXls(typeof(TobePaidOverviewEntity), filterBucket);
            File.WriteAllBytes(@"d:\test\test.xlsx", spreadSheet);


Now the GlobalService class is designed for using WCF:

    [ServiceContract(Namespace = "http://Peopleware/Osiris")]
    public interface IGlobalService
        [OperationContract, NetDataContract]
        byte[] ExportToXls(Type type, IRelationPredicateBucket filterBucket);

        InstanceContextMode = InstanceContextMode.PerCall,
        UseSynchronizationContext = false,
        ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class GlobalService : IGlobalService
        public byte[] ExportToXls(Type entityType, IRelationPredicateBucket filterBucket)
           return Export.CreateXlsFile(typeof(TobePaidOverviewEntity), filterBucket, 20000, new SortExpression(TobePaidOverviewFields.Id | SortOperator.Ascending));

Calling the method ExportToXls from a client works for a low number of rows, but trying to export more then 7500 rows gives me a StackOverflowExcpetion.  I tried to execute the export on another thread with an increased StackSize (It seems that the worker threads of WCF doesn't use the default size of 1MB of stacksize) but it didn't solve it.  Any one have an idea to find the cause?


The exception occurs at:

// file ExcelWorkSheet.cs

        private void UpdateRowCellData(StreamWriter sw)
             sw.Write("", cell.CellAddress, (num3 < 0L) ? 0L : num3);
When writing a cell containing a DB.Null value.


Jan 29, 2013 at 9:31 PM
Edited Jan 29, 2013 at 9:44 PM

I ran into the same problem yesterday with version 3.1.2. Building the WCF service in Release configuration fixed it for me. Still not certain why it won't work in a debug build of my WCF service.