M
M
mletov2017-06-05 14:49:58
ASP.NET
mletov, 2017-06-05 14:49:58

Why does the page remember the previously created Singleton when generating reports (npoi)?

An interesting thing
. I have an action in the controller to generate an xlsx file using NPOI something like this

public ActionResult GenerateReport(DateTime? dtStart, DateTime? dtFinish)
{
            //...

           //Создаем книгу через Singleton
           hssfwb = Hsswb.getInstance();
       
          //...

           //Отдаем результат
            MemoryStream output = new MemoryStream();
            HSSFFormulaEvaluator.EvaluateAllFormulaCells(hssfwb);
            hssfwb.Write(output);
            return File(output.ToArray(), "application/vnd.ms-excel", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");
}

Work with the book is implemented through Singleton
protected static XSSFWorkbook hssfwb { get; set; }
     
        class Hsswb
        {
            private static XSSFWorkbook instance;

            private Hsswb()
            {

            }

            public static XSSFWorkbook getInstance()
            {
                if (instance == null)
                {
                    instance = new XSSFWorkbook();
                }
                return instance;
            }
        }

The report generation is called like this /Home/GenerateReport?dtStart=2017-04-10&dtStart=2017-04-17
Only the dates change.
And it turns out that after the first report generation, all the rest are generated with the same parameters with which the report was generated for the first time, because this check
if (instance == null)
                {
                    instance = new XSSFWorkbook();
                }

gives that instance is not null, but the instance created for the first time.
How can this happen? In my understanding, if I reload the page, and even with other GET parameters, all class instances must be created anew, where does the old instance come from?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arkadiy Parinov, 2017-06-05
@mletov

So that's what it is and Singleton =) Even if it is called from another controller, the instance will be the same that was created at the very first call.
You don't seem to need this at all, why not use new XSSFWorkbook() every time?

A
Anvar Shakhmaev, 2017-06-05
@RxR

In my understanding, if I reload the page, and even with other GET parameters, all class instances must be created anew, where does the old instance come from?

Singleton exists at the application level, not at the request level of a single user.

D
Dmitry Kovalsky, 2017-06-05
@dmitryKovalskiy

You do not have a singleton, but it is not clear what. The implementation of the Singleton pattern must be thread-safe. In your case, it is not. Either read more about the implementation of the pattern, or pick up an IoC container and ask it to control the number of objects of this class (i.e. no more than 1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question