2013-10-14 60 views
3

我試圖呈現一個html代碼,將其導出到圖像,然後使用該圖像將其放入一個pdf文檔。我正在做一個asp.net/C# web應用程序。我遇到的問題是,當我做一個以上的時間把它給我這個錯誤:我可以在Web應用程序中使用Awesomeium嗎?

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呈現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. 

任何想法我該怎麼做?

感謝

+0

從你所描述的'WebCore.Initialize'應該在'Application_Start'中,'WebCore.Shutdown'應該放在'Global.asax'的'Application_End'中。 – Romoku

+1

我這樣做,它給了我第二個錯誤:試圖讀取或寫入受保護的內存。這通常表明其他內存已損壞。當前線程當前不在運行代碼或無法獲取調用堆棧。 – robert00769

回答

4

簡短的回答:是的,可以。

但是根據WebCore documentation

Note that Awesomium and Awesomium.NET are not thread-safe. All API members should be called from the thread that initialized the WebCore (see Initialize(WebConfig)) or created the first WebView, WebSession or technology specific WebControl.

也期待在的WebCore.Update() method descriptionExceptions部分:

System.AccessViolationException - You attempted to access the member from a thread other than thread where WebCore was created.

因此考慮到在網絡應用程序的每個請求在不同的線程中通常處理,AccessViolationException是有記錄的特徵

因此,您在這裏的任務是確保所有Awesomium調用將在特殊的專用線程中處理。該線程應該在應用程序級別上運行,並且它的訪問必須在請求之間共享。從技術上講,您需要編寫一種消息循環或同步上下文,您可以將您的delegateActionFunc與Awesomium代碼相提並論,以便僅在此線程中提供其執行。

+0

你能提供一個例子嗎? – Exzile

0

您可能需要調用webview.invoke或webview.begininvoke,並且您將需要創建一個委託來訪問在另一個線程中創建webcore.initialize之前創建的webview,我將使用單例模式使用相同的webview實例。

相關問題