我可以帮您翻译成中文:我能在Web应用程序中使用Awesomeium吗?

3
我正在尝试渲染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.

有什么想法我该怎样做这个?谢谢。

根据您的描述,WebCore.Initialize 应该放在 Application_Start 中,而 WebCore.Shutdown 应该放在 Global.asaxApplication_End 中。 - Dustin Kingen
1
我尝试了这个,但是它给了我第二个错误:尝试读取或写入受保护的内存。这通常表明其他内存已经损坏。当前线程没有运行代码或无法获取调用堆栈。 - robert
2个回答

4
简短回答:可以。
但是根据 WebCore documentation
请注意,Awesomium和Awesomium.NET不是线程安全的。 所有API成员都应该从初始化WebCore(参见Initialize(WebConfig))或创建第一个WebView,WebSession或技术特定的WebControl的线程中调用。
还要查看 WebCore.Update() method descriptionExceptions部分: System.AccessViolationException - 您尝试从创建WebCore的线程之外的线程访问成员。
因此,考虑到Web应用程序中的每个请求通常在不同的线程中处理,AccessViolationException是一种文档化的feature
因此,您在这里的任务是确保所有 Awesomium 调用都将在一个特殊的专用线程中处理。该线程应该在应用程序级别上运行,并且其访问必须在请求之间共享。从技术上讲,您需要编写一种消息循环或同步上下文,在其中您将能够推送包含 Awesomium 代码的委托、操作或函数,以便仅在此线程中执行。

你能提供一个例子吗? - Exzile

0

可能你需要调用webview.invoke或webview.begininvoke,你还需要创建一个委托来访问在另一个线程中在webcore.initialize之前创建的webview,我会使用单例模式来使用相同的webview实例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接