0

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

エラー内容

 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()

発生箇所のコード

            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();
        }

Generateメソッドの中身

            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;
        }
9
  • 再現しないとなると、ここでは解決できない可能性があります(解決したかどうかわからないため)。見た限り、 currentPageText が null になることを想定している一方で.ToString() を直接呼び出しているのが気になります。
    – v..snow
    2022年7月4日 13:05
  • 何を何で作っているか書けませんか? (例: ASP.NET MVC アプリを Visual Studio 2022 を使ってフレームワーク .NET Framework 4.8 で作ってます)
    – WebSurfer
    2022年7月4日 23:03
  • タイトルに「async/await・・・」とありますが、元々同期で async await を使う意味がなさそうですけど? それがあるから問題と言うわけではないかもしれませんが・・・
    – WebSurfer
    2022年7月4日 23:22
  • コメントありがとうございます。使い慣れておらず失礼がありましたら申し訳ありません。 @v..snow 様 解決できない可能性については残念ながら承知しております。その上でご指摘のような気になる点を示していただけるのはとても助かります。ありがとうございます。
    – SuimA7
    2022年7月5日 5:57
  • @WebSurfer 様 基本的なことが出来ておらず申し訳ありませんでした。以後、気をつけます。 ASP.NET MVC アプリをVisualStudio2017を使って.NetFramework4.7.2で作成しております。PDFの生成にはComponentOneという製品を利用しております。
    – SuimA7
    2022年7月5日 5:57

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。