@@ -9,13 +9,13 @@ namespace Codeuctivity.PuppeteerSharp
99 /// <summary>
1010 /// Renders HTML files
1111 /// </summary>
12- public class Renderer : IAsyncDisposable
12+ public class Renderer : IAsyncDisposable , IDisposable
1313 {
1414 private Browser Browser { get ; set ; } = default ! ;
1515 private int LastProgressValue { get ; set ; }
1616
1717 /// <summary>
18- /// Browserfetcher - used to get chromium bins
18+ /// Browser fetcher - used to get chromium bins
1919 /// </summary>
2020 public BrowserFetcher BrowserFetcher { get ; private set ; } = default ! ;
2121
@@ -54,7 +54,6 @@ private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
5454 /// </summary>
5555 /// <param name="sourceHtmlFilePath"></param>
5656 /// <param name="destinationPdfFilePath"></param>
57- /// <returns></returns>
5857 public async Task ConvertHtmlToPdf ( string sourceHtmlFilePath , string destinationPdfFilePath )
5958 {
6059 if ( ! File . Exists ( sourceHtmlFilePath ) )
@@ -63,7 +62,7 @@ public async Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destination
6362 }
6463
6564 var absolutePath = Path . GetFullPath ( sourceHtmlFilePath ) ;
66- var page = await Browser . NewPageAsync ( ) . ConfigureAwait ( false ) ;
65+ await using var page = await Browser . NewPageAsync ( ) . ConfigureAwait ( false ) ;
6766 await page . GoToAsync ( $ "file://{ absolutePath } ") . ConfigureAwait ( false ) ;
6867 await page . PdfAsync ( destinationPdfFilePath ) . ConfigureAwait ( false ) ;
6968 }
@@ -73,7 +72,6 @@ public async Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destination
7372 /// </summary>
7473 /// <param name="sourceHtmlFilePath"></param>
7574 /// <param name="destinationPngFilePath"></param>
76- /// <returns></returns>
7775 public async Task ConvertHtmlToPng ( string sourceHtmlFilePath , string destinationPngFilePath )
7876 {
7977 if ( ! File . Exists ( sourceHtmlFilePath ) )
@@ -82,9 +80,9 @@ public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destination
8280 }
8381
8482 var absolutePath = Path . GetFullPath ( sourceHtmlFilePath ) ;
85- var page = await Browser . NewPageAsync ( ) . ConfigureAwait ( false ) ;
83+ await using var page = await Browser . NewPageAsync ( ) . ConfigureAwait ( false ) ;
8684 await page . GoToAsync ( $ "file://{ absolutePath } ") . ConfigureAwait ( false ) ;
87- await page . ScreenshotAsync ( destinationPngFilePath , new ScreenshotOptions ( ) { FullPage = true } ) . ConfigureAwait ( false ) ;
85+ await page . ScreenshotAsync ( destinationPngFilePath , new ScreenshotOptions { FullPage = true } ) . ConfigureAwait ( false ) ;
8886 }
8987
9088 private void DownloadProgressChanged ( object sender , DownloadProgressChangedEventArgs e )
@@ -95,15 +93,50 @@ private void DownloadProgressChanged(object sender, DownloadProgressChangedEvent
9593 }
9694 }
9795
98- ValueTask IAsyncDisposable . DisposeAsync ( )
96+ /// <summary>
97+ /// Dispose
98+ /// </summary>
99+ public void Dispose ( )
100+ {
101+ Dispose ( disposing : true ) ;
102+ GC . SuppressFinalize ( this ) ;
103+ }
104+
105+ /// <summary>
106+ /// DisposeAsync
107+ /// </summary>
108+ public async ValueTask DisposeAsync ( )
99109 {
100- if ( Browser == null )
110+ await DisposeAsyncCore ( ) ;
111+
112+ Dispose ( disposing : false ) ;
113+ #pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
114+ GC . SuppressFinalize ( this ) ;
115+ #pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
116+ }
117+
118+ /// <summary>
119+ /// Dispose
120+ /// </summary>
121+ /// <param name="disposing"></param>
122+ protected virtual void Dispose ( bool disposing )
123+ {
124+ if ( disposing )
101125 {
102- return new ValueTask ( ) ;
126+ Browser ? . Dispose ( ) ;
103127 }
128+ }
104129
105- Browser . CloseAsync ( ) . ConfigureAwait ( false ) ;
106- return ( ( IAsyncDisposable ) Browser ) . DisposeAsync ( ) ;
130+ /// <summary>
131+ /// DisposeAsync
132+ /// </summary>
133+ protected virtual async ValueTask DisposeAsyncCore ( )
134+ {
135+ if ( Browser is not null )
136+ {
137+ await Browser . CloseAsync ( ) ;
138+ await Browser . DisposeAsync ( ) ;
139+ }
107140 }
108141 }
109142}
0 commit comments