Answer the question
In order to leave comments, you need to log in
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");
}
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;
}
}
if (instance == null)
{
instance = new XSSFWorkbook();
}
Answer the question
In order to leave comments, you need to log in
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?
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?
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 questionAsk a Question
731 491 924 answers to any question