HakanL / WkHtmlToPdf-DotNet

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
GNU Lesser General Public License v3.0
367 stars 66 forks source link

EF Core migrations fails after adding this package #18

Closed andbjer closed 3 years ago

andbjer commented 3 years ago

We currently use this package to generate PDFs from HTML in a .NET Core Web API and it has been working great.

When I try to add a new EF Core migration an unhandled exception is thrown in this package:

dotnet ef migrations add <name> 
Build started...
Build succeeded.
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at DinkToPdf.PdfTools.Dispose(Boolean disposing)
   at DinkToPdf.PdfTools.Finalize()

In Startup.cs I have added the IConverter as a singleton:

    .AddSingleton<IConverter>(new SynchronizedConverter(new PdfTools()))

Migrations does not work in either Windows or Mac.

Related: #14

HakanL commented 3 years ago

Please see if the fix I just pushed solved the issue.

andbjer commented 3 years ago

What's the easiest way for me to test this?

I've pulled down the code from the repository and added the project to my solution. I added every runtime manually in the same folder structure as it is listed in DinkToPdf.csproj:

    <None Include="runtimes\linux-x64\native\libwkhtmltox.so" Pack="true" PackagePath="runtimes\linux-x64\native" />
    <None Include="runtimes\linux-x86\native\libwkhtmltox.so" Pack="true" PackagePath="runtimes\linux-x86\native" />
    <None Include="runtimes\osx-x64\native\libwkhtmltox.dylib" Pack="true" PackagePath="runtimes\osx-x64\native" />
    <None Include="runtimes\win-x64\native\wkhtmltox.dll" Pack="true" PackagePath="runtimes\win-x64\native" />
    <None Include="runtimes\win-x86\native\wkhtmltox.dll" Pack="true" PackagePath="runtimes\win-x86\native" />

Now I am getting this error when i try to generate a PDF:

System.DllNotFoundException: Unable to load shared library 'wkhtmltox' or one of its dependencies
adamhathcock commented 3 years ago

You probably shouldn't even be loading this in EFCore migration.

Change your DI definition to be lazy:

services.AddSingleton<IConverter>(x => new SynchronizedConverter(new PdfTools()))
HakanL commented 3 years ago

I uploaded a pre-release to NuGet, try that. https://www.nuget.org/packages/Haukcode.DinkToPdf/1.1.3-test-dispose-fix

andbjer commented 3 years ago

Tested the pre-release and migrations is working again. Thank you!

I also tested the suggestion by @adamhathcock before upgrading to the pre-release. This also solved my problem.

Thank you both!

HakanL commented 3 years ago

Ah, perfect, thanks for the quick feedback @andbjer ! Please use the new repo , version 1.2 which includes this fix.