DinkToPdf Net Core not able to load DLL files

爱⌒轻易说出口 提交于 2021-01-27 13:46:23

问题


I am try to generate PDF from html sql server database using DinkToPdf library.

In the Startup file I have added

var context = new CustomAssemblyLoadContext();
context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll"));

The line gives me error on launching the web app

DllNotFoundException: Unable to load DLL 'C:\Program Files\IIS Express\libwkhtmltox.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(string unmanagedDllPath)

DllNotFoundException: Unable to load DLL 'C:\Program Files\IIS Express\libwkhtmltox.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Kindly assist wherever you can


回答1:


Just in case anyone else is having the same issue I was able to solve it by installing Microsoft Visual C++ 2015 Redistributable.




回答2:


It is mentioned in library's git repo that you should download binaries and include them in your source code:

Copy native library to root folder of your project. From there .NET Core loads native library when native method is called with P/Invoke. You can find latest version of native library here. Select appropriate library for your OS and platform (64 or 32 bit).

What was breaking things was that I was going to that url and right click on each file and select save link as(chrome). This leads to a broken file being downloaded:

DON'T DO THAT you have to open each file within github and then use that Download button. The healthy file is much bigger than what you would get if you go the wrong way!

ridiculous but the problem may be caused by this ...




回答3:


I found some work-arounds. They are not perfect but worth a try, and they did do help and I was able to generate PDFs from SQl Server. I put the .dll files in the following folder and it worked.

C:\Program Files\IIS Express

and the loaded the .dll files with

Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll");

The other way I went for the whole Path

context.LoadUnmanagedLibrary(Path.GetFullPath(@"C:\Users\User\source\repos\WebSolution\WebApp\libwkhtmltox.dll"));

Both of them worked. However, I urge Net Core developers to work on the GetCurrentDir very well. Or a Method to load from the Project or Solution Folder

Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll");



回答4:


On Asp.Net Core application I use it like this to get the current directory on runtime

#if DEBUG
            //windows
            string filePath = $@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\libwkhtmltox.dll";
#else
            //linux
            string filePath = @$"{(($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/libwkhtmltox.so").Replace(@"\", @"/"))}";
#endif
            CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
            context.LoadUnmanagedLibrary(filePath);
            serviceCollection.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
#endregion


来源:https://stackoverflow.com/questions/55430532/dinktopdf-net-core-not-able-to-load-dll-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!