我可以使用Awesomeium一个web应用程序中? [英] Can I use Awesomeium within a web application?

查看:477
本文介绍了我可以使用Awesomeium一个web应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图呈现一个HTML code将其导出到一个图像,然后使用该形象把它pdf文件英寸我做在asp.net/C# Web应用程序的所有。我遇到的问题是,当我做了超过一次它给我这个错误:

I'm trying to render a html code to export it to an image and then use that image to put it in a pdf document. I'm doing all in an asp.net/C# web application. The problem I'm having is that when I do that more than one time it give me this error:

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.

这是我的code:

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。这样做后,当我调试,另一个错误想出了:

I figure that I can't initialize the WebCore every time the web page render, so I put it in the Global.asax. After doing this, when I'm debugging, another error came up:

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.

任何想法我怎么能这样做呢?

Any ideas how can I do this?

感谢

推荐答案

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

Short answer: yes, you can.

但根据 的WebCore 文档

注意Awesomium和Awesomium.NET不是线程安全的。所有API
  成员应当从初始化的WebCore的线程调用
  (见初始化(WebConfig))或创建的第一个web视图,WebSession
  或技术特定WebControl的。

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.

例外部分相对=nofollow > WebCore.Update()方法描述

Also look at the Exceptions section of WebCore.Update() method description:

System.AccessViolationException - 您尝试访问
  从会员那里比WebCore的创建线程的线程上。

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

因此​​,考虑到在Web应用程序中的每个请求在不同的线程处理一般, AccessViolationException 是文档的功能

Thus considering that every request in web application is generally processed in different thread, AccessViolationException is a documented feature.

因此​​在这里你的任务是保证所有Awesomium调用将在一个特殊的专用线程处理。该线程应该住在应用水平和它的访问已被请求之间共享。从技术上讲,你需要写一种消息循环或同步环境,在那里你可以把你的委托动作函数功能与Awesomium code提供其执行只有在这个线程。

Therefore your task here is to guarantee that all Awesomium calls will be processed in a special dedicated thread. The thread should live on application level and its access has to be shared between requests. Technically, you need to write a kind of Message Loop or Synchronization Context where you will be able to push your delegate, Action or Func with Awesomium code to provide its execution only in this thread.

这篇关于我可以使用Awesomeium一个web应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆