我可以在Web应用程序中使用Awesomeium吗?

本文关键字:Awesomeium Web 应用程序 我可以 | 更新日期: 2023-09-27 18:35:21

我正在尝试渲染一个html代码以将其导出到图像中,然后使用该图像将其放入pdf文档中。我正在一个 asp.net/C#的Web应用程序中完成所有操作。我遇到的问题是,当我多次这样做时,它会给我这个错误:

You are attempting to re-initialize the WebCore. The WebCore must only be initialized once per process and must be shut down only when the process exits.

这是我的代码:

WebCore.Initialize(new WebConfig(), false);
using (WebView webView = WebCore.CreateWebView((pdfWidth - 80).ToString().ToInt() + 20, (pdfHeight / 2).ToString().ToInt() + 20))
{
    webView.LoadHTML(html);
    while (webView.IsLoading || !webView.IsDocumentReady)
                WebCore.Update();
    Thread.Sleep(1100);
    WebCore.Update();
    string fileName = Server.MapPath("result." + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + ".png");
    BitmapSurface surface = (BitmapSurface)webView.Surface;
    surface.SaveToPNG(fileName, true);
    WebCore.Shutdown();
}

我认为我无法在每次网页呈现时初始化WebCore,所以我把它放在Global.asax中。这样做后,当我调试时,出现了另一个错误:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt. The current thread is not currently running code or the call stack could not be obtained.

任何想法我该怎么做?

谢谢

我可以在Web应用程序中使用Awesomeium吗?

简短的回答:是的,你可以。

但根据WebCore文档:

请注意,Awesomium 和 Awesomium.NET 不是线程安全的。所有接口 成员应该从初始化 WebCore 的线程调用 (参见初始化(WebConfig))或创建了第一个WebView,WebSession 或特定于技术的 Web 控件。

另请查看WebCore.Update()方法说明的Exceptions部分:

System.AccessViolationException - 您试图访问 来自创建 WebCore 的线程以外的线程的成员。

因此,考虑到 Web 应用程序中的每个请求通常在不同的线程中处理,AccessViolationException是一个记录在案的功能

因此,您在此处的任务是保证所有 Awesomium 调用都将在特殊的专用线程中处理。线程应位于应用程序级别,并且必须在请求之间共享其访问权限。从技术上讲,您需要编写一种消息循环或同步上下文,您可以在其中使用 Awesomium 代码推送您的delegateActionFunc,以仅在此线程中提供其执行。

可能需要

调用webview.invoke或webview.begininvoke,并且您需要创建一个委托来访问在webcore.initialize之前创建的Web视图 在另一个线程中,我会使用单例模式来使用相同的Webview实例。