0
votes

I'm using Azure app service to host an API. The API contains a dummy method that calls a normal Webbroswer. Below is code :

[HttpGet]
    [Route("c")]
    public string GetCorrection()
    {
        string result = "NoResults";
        ClassLibrary2.Class2 class2 = new ClassLibrary2.Class2();
        try
        {
            Thread thread = new Thread(new ThreadStart(() =>
            {
                class2.Browse();

            }));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }

        return result;
    }


public string Browse()
        {
            using (WebBrowser browser = new WebBrowser())
            {
                browser.ScrollBarsEnabled = false;
                browser.ScriptErrorsSuppressed = true;
                browser.AllowNavigation = true;

                browser.Navigate("https://en.wikipedia.org/wiki/Wiki");

                return browser.DocumentTitle;
            }
        }
}

Eveything works normaly localy. But once i publish it to Azure, 502 error occurs. enter image description here If i remove the browser.Navigate("https://en.wikipedia.org/wiki/Wiki"); line, no error occurs. Any idea ? I'm having doubts that's it's related to the System.Windows.Forms event.

Thanks in advance,

1
Just out of curiosity, why are you using a WebBrowser object? Wouldn't it be easier to use a HttpClient to get the document title? See stackoverflow.com/questions/36803819/… - Rui Jarimba
@RuiJarimba This is a dummy test. My original code opens an HTML page, and on the document.loadComplete event, javascript function is called - user3012488
@RuiJarimba Do you have any idea if Chromium (CefSharp) can be a solution ? - user3012488
Sorry I have absolutely no idea if it would work. Have you considered using SignalR? codemag.com/article/1210071/… - Rui Jarimba

1 Answers

0
votes

As Rui Jarimba commented above, we can't use WebBrowser controller in Azure WebService: Using WebBrowser control in an Azure webjob

As a workaround ,we can use HttpClient to achieve this, here is a simple demo for your reference:

    /// <summary>
    /// Get Web Title By HttpClient
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string GetWebTitleByHttpClient(string url)
    {           
        HttpClient httpclient = new HttpClient();           
        var task = httpclient.GetAsync(url);
        task.Wait();
        HttpResponseMessage message = task.Result;

        Stream myResponseStream = message.Content.ReadAsStreamAsync().Result;
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        String result = myStreamReader.ReadToEnd();
        int startIndex = result.IndexOf("<title>") + 7;
        int endIndex = result.IndexOf("</title>");
        if (endIndex > startIndex)
        {
            string title = result.Substring(startIndex, endIndex - startIndex);
            return title;
        }
        else
        {
            return null;
        }

    }

Here are the screenshots of result that I tested:

enter image description here

enter image description here

Update

I am sorry to misunderstand that you want to call JavaScript functions when the web finish loading. According to your issue, i recommend you to use Azure VM or Cloud Service to do it because there is no such limitation.

More information about Azure VM for your reference: Virtual Machines Documentation

More information about Azure Cloud Service: Cloud Services Documentation