C# Qt:无法在C上初始化OLE(错误80010106)-(libwkhtmltox.dll)#

C# Qt:无法在C上初始化OLE(错误80010106)-(libwkhtmltox.dll)#,c#,asp.net-core-mvc,wkhtmltopdf,C#,Asp.net Core Mvc,Wkhtmltopdf,我正在使用这个libwkhtmltox.dll将我的html转换成pdf。我正在使用c#netCore 用法: private static string CreatePdf(string content, string fileName) { var fullPath = $"{_projectSettingsOptions.Pdf}/{fileName}"; using (var sw = new StreamWriter(new FileS

我正在使用这个libwkhtmltox.dll将我的html转换成pdf。我正在使用c#netCore

用法:

    private static string CreatePdf(string content, string fileName)
    {
        var fullPath = $"{_projectSettingsOptions.Pdf}/{fileName}";

        using (var sw = new StreamWriter(new FileStream(fullPath, FileMode.Create), Encoding.UTF8))
        {
            sw.Write(content);
            sw.Flush();
        }

        new Pdf(_projectSettingsOptions).Convert(fullPath, content);

        return fullPath;
    }
关于上述代码:

  • 内容=我要转换为pdf的html内容
  • 完整路径=存储html内容的位置
  • \u项目设置选项=项目的路径,如图像路径、pdf路径等
然后调用Pdf类,传递要转换的路径和内容

new Pdf(_projectSettingsOptions).Convert(fullPath, content);
这是我的Pdf课程:

namespace SiteMFPManager.Library.Util
{

using Assembly;
using DinkToPdf;
using Settings;
using System.IO;

public class Pdf
{
    public Pdf(ProjectSettingsOptions projectSettingsOptions) =>
        new CustomAssemblyLoadContext().LoadUnmanagedLibrary(Path.Combine(projectSettingsOptions.References, "libwkhtmltox", "libwkhtmltox.dll"));

    public void Convert(string fullPath, string content) =>
        new SynchronizedConverter(new PdfTools()).Convert(new HtmlToPdfDocument
        {
            GlobalSettings =
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings { Top = 10 },
                Out = fullPath,
            },
            Objects =
            {
                new ObjectSettings
                {
                    PagesCount = true,
                    HtmlContent = content,
                    WebSettings = { DefaultEncoding = "utf-8" }
                }
            }
        });
    }
}
CreatePdf方法执行了两次。在第一次执行时,它在控制台中执行并向我显示:Qt:无法初始化OLE(错误80010106),当代码第二次执行时,应用程序停止,没有异常发生,没有任何。。。就是那个消息

如果你需要更多的信息来帮助我解决这个问题,就告诉我


抱歉,如果帖子格式不正确,我是这方面的新手…

要使SynchronizedConverter在.Net Core中正常工作,您需要将其注册为Singleton(很可能是在Startup.cs ConfigureServices中):

(此处也有描述:)

之后,您可以将转换器注入控制器,而不是创建新实例。
这样,DLL总是在同一个线程中被调用。

将属性[STAThread]设置为CreatePdf方法


您可能仍然会在控制台上看到错误,但至少会创建pdf。

可能的重复与我的问题无关。请阅读Hans Passant的评论。你的问题中所描述的一切都与你的问题无关,除了错误代码,真的。你解决过这个问题吗?
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));