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

我正在尝试渲染一个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文档 :

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

另请WebCore.Update()方法描述的Exceptions部分:

System.AccessViolationException – 您试图从创建WebCore的线程以外的线程访问该成员。

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

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

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