关于c#:DinkToPdf Net Core无法加载DLL文件

DinkToPdf Net Core not able to load DLL files

我尝试使用DinkToPdf库从html sql服务器数据库生成PDF。

在启动文件中,我添加了

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

该行使我在启动Web应用程序时出错

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)

竭诚为您服务


万一其他人遇到相同的问题,我可以通过安装Microsoft Visual C ++ 2015 Redistributable解决此问题。


库的git repo中提到您应该下载二进制文件并将其包含在源代码中:

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

造成问题的是,我要转到该URL,然后右键单击每个文件并选择save link as(chrome)。这将导致下载损坏的文件:
enter image description here

不要那样做
您必须在github中打开每个文件,然后使用Download按钮。
健康的文件比错误处理的文件大得多!

enter image description here

荒谬,但问题可能是由...


在Asp.Net Core应用程序上,我像这样使用它来在运行时获取当前目录

1
2
3
4
5
6
7
8
9
10
11
#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


我找到了一些解决方法。它们并不完美,但是值得一试,它们确实提供了帮助,并且我能够从SQl Server生成PDF。我将.dll文件放在以下文件夹中,它可以正常工作。

C:\\Program Files\\IIS Express

并用

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

我走遍整个道路的另一种方式

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

他们俩都工作了。但是,我敦促Net Core开发人员在GetCurrentDir上做得很好。或从项目或解决方案文件夹加载的方法

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