0

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,

  • 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 https://stackoverflow.com/questions/36803819/how-get-webpages-title-when-they-are-encoded-differently – Rui Jarimba Jul 16 '18 at 13:03
  • @RuiJarimba This is a dummy test. My original code opens an HTML page, and on the document.loadComplete event, javascript function is called – user3012488 Jul 16 '18 at 13:15
  • 2
    This might be related: https://stackoverflow.com/questions/32969738/using-webbrowser-control-in-an-azure-webjob – Rui Jarimba Jul 16 '18 at 13:17
  • @RuiJarimba Do you have any idea if Chromium (CefSharp) can be a solution ? – user3012488 Jul 16 '18 at 13:30
  • Sorry I have absolutely no idea if it would work. Have you considered using SignalR? https://www.codemag.com/article/1210071/The-Simplest-Thing-Possible-Creating-Push-Notifications-with-SignalR – Rui Jarimba Jul 16 '18 at 13:32
  • SignalR is not usueful in my case, anw thanks for the link. – user3012488 Jul 16 '18 at 13:37
  • You need to run a message loop in your STA thread. I answered similar questions, search SO for `MessageLoopApartment`. – noseratio Jul 16 '18 at 14:21
  • Or you can use a headless browser, see here: http://toolsqa.com/selenium-c-sharp/ – Sasha Jul 17 '18 at 07:43

1 Answers1

0

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

Lee Liu
  • 1,981
  • 1
  • 12
  • 13
  • 1
    In comments he said he is calling a JavaScript function, this may be the reason for the use of WebBrowser. – Sasha Jul 17 '18 at 07:43