Attempted to read or write protected memory. Only when using WCF? Awesomium/HTML Agility Pack

awesomium c# html-agility-pack wcf

Question

I have a form that uses WCF to transfer data to another process after scraping it from a webpage using Awesomium as the browser and HTMLAgilityPack to pass the HTML.

When I use the ScraperForm technique. The button on the form's GetData() function returns no errors (the html is grabbed properly by the javascript).

I receive the following problem in my ScraperForm when I call it from my other form (using the WCF client):

public partial class ScraperForm : Form
{
    public string Html;
    public HtmlAgilityPack.HtmlDocument HtmlDoc;

    ServiceHost Host;
    ModelDataServer DataServer;

    public ScraperForm()
    {
        InitializeComponent();

        #region Start Data Server
        DataServer = new ModelDataServer();
        DataServer._GetData = new ModelDataServer.GetData(this.GetData);

        Host = new ServiceHost(DataServer, new Uri[]
            {
                new Uri("http://localhost:8000")
            });

        Host.AddServiceEndpoint(typeof(IModelData),
            new BasicHttpBinding(),
            "ModelData");

        Host.Open();

        #endregion

        HtmlDoc = new HtmlAgilityPack.HtmlDocument();
        Html = "";
    }

    private void CloseSever()
    {
        Host.Close();
    }

    public ArrayList GetData()
    {
        #region HTMLScrape - HTML Agility Pack

        // navigate to website
        ArrayList ret = new ArrayList();
        Html = webControl.ExecuteJavascriptWithResult("document.documentElement.outerHTML").ToString();
        // *********** Breaks on the line above this! ***********

    // Grab data out from html, add to ret (ArrayList) 

    return ret;        
    }

    private void button1_Click(object sender, EventArgs e)
    {
        GetData();   // Causes No Errors!
    }
}

My code for WCF Interprocess Communication

[ServiceContract]
public interface IModelData
{
    [OperationContract]
    ArrayList GetData();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ModelDataClient
{
    ChannelFactory<IModelData> HttpFactory;
    IModelData HttpProxy;

    public ModelDataClient()
    {
        HttpFactory = new ChannelFactory<IModelData>(
            new BasicHttpBinding(),
            new EndpointAddress("http://localhost:8000/ModelData"));

        HttpProxy = HttpFactory.CreateChannel();
    }

    public ArrayList GetData()
    {
        return HttpProxy.GetData();
    }
}

[ServiceBehavior(UseSynchronizationContext = false, InstanceContextMode = InstanceContextMode.Single)]
public class ModelDataServer : IModelData
{
    public delegate ArrayList GetData();
    public GetData _GetData { get; set; }

    public ModelDataServer()
    {
    }

    public ArrayList GetData()
    {
        return _GetData();   // When called here (Using WCF) it causes the error!
    }
}

(Html = webControl.ExecuteJavascriptWithResult) error ("document.documentElement.outerHTML"). ToString();):

Attempted to read or write protected memory. This is often an indication that other memory is corrupt

I believe that my scraper form's setting to run as administrator or anything has something to do with this. I appreciate your support.

Update:

I turned off Data Execution Prevention as instructed here (http://jtstroup.net/post/Attempted-to-read-or-write-protected-memory-This-is-often-an-indication-that-other-memory-is-corrupt.aspx), but I continue to get the same issue.

I think there is a mismatch between Awesomium and the rest of my software, which is where the issue is. Possibly something to do with a different version of.net.

Building the whole project as x86 was another proposal I came across, but it hasn't worked either.

Anyone have any recommendations I could try? How can I switch to using Awesomium as my target.net version? Will this function?

1
1
2/17/2014 4:45:58 PM

Accepted Answer

I resolved the issue by collecting the HTML from the webform on a timer, after which my WCF just calls a method that pulls the most recent data from the timer's last tick.

Therefore, it seems probable that the WebCore component's lack of thread safety is the cause of the issue.

Greetings, groverboy!

0
12/19/2013 11:05:08 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow