

C#で実装中の処理で以下のエラーが発生するのですが、発生理由の特定ができておらず、デバッグしようにも再現ができていません。  
考えられるエラー内容またはエラーの発生条件について少しでも分かることがあればアドバイスいただけませんでしょうか。  
どうぞよろしくお願いいたします。

#### エラー内容

[code]

     9372 2022-XX-XX XX:XX:XX ERROR XXXX.Web.HttpModule - Application error.
    Exception: System.Web.HttpUnhandledException
    Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
    Source: System.Web
       at System.Web.UI.Page.HandleError(Exception e)
       at System.Web.UI.Page.<ProcessRequestAsync>d__554.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Web.TaskAsyncHelper.EndTask(IAsyncResult ar)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    
    Nested Exception
    
    Exception: System.NullReferenceException
    Message: Object reference not set to an instance of an object.
    Source: XXXX.XXXXProject.PDFGenerater
       at XXXX.XXXXProject.PDFGenerater.Api.Generater.<Generate>d__34.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at ASP.XXXXProject_PdfDownload_ascx.<AsyncInPage>d__1.MoveNext() in c:\XXXXProject\XXXX\Website\XXXXProject\PdfDownload.ascx:line 99
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Web.UI.PageAsyncTaskManager.<ExecuteTasksAsync>d__3.MoveNext() 
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Web.UI.Page.<ProcessRequestAsync>d__554.MoveNext()
    
[/code]

#### 発生箇所のコード

[code]

                using (var memoryStream = await generator.Generate(viewModel, language))
            {
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AppendHeader("content-disposition", string.Format("inline; filename={0}.pdf", partno));
                Response.OutputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
    
[/code]

#### Generateメソッドの中身

[code]

                public async Task<MemoryStream> Generate(PdfViewModel viewModel, Language language)
            {
                var pdfDocument = new C1PdfDocument()
                { 
                    ImageQuality = ImageQualityEnum.High,
                    FontType = FontTypeEnum.Embedded
                };
                pdfDocument.Clear();
    
                var pdfFont = new PdfFont(viewModel.RegionCode);
    
                //ヘッダー、フッターの呼び出し
                CreateContent(pdfDocument, viewModel, pdfFont);
    
                //ページ番号の取得
                int pageCounter = 1;
                for (int page = 0; page < pdfDocument.Pages.Count; page++)
                {
                    var currentPageText = viewModel.Footer.FooterPageText
                    ?.Replace("{0}", pageCounter.ToString())
                    ?.Replace("{1}", pdfDocument.Pages.Count.ToString());
    
                    pdfDocument.CurrentPage = page;
    
                    pdfDocument.DrawString(
                       currentPageText.ToString(),
                       pdfFont.Font12px,
                       footerBrush,
                       new Rectangle(new Point(530, 680), new Size(120, 20))
                   );
                    pageCounter++;
                }
    
                MemoryStream ms = new MemoryStream();
                pdfDocument.Save(ms);
                pdfDocument.Dispose();
                return ms;
            }
    
[/code]


CC BY-SA 4.0
2022-07-04T11:47:29.613
89756
2022-07-04T12:31:05.190
2022-07-04T12:31:05.190
3060
53348
question
c# pdf stream
C#のAsync/Awaitの処理で発生するエラーの特定方法