0

I am learning how to use the .NET framework. I am working with ASP .NET core. I have never had or hit my azure webhosting quota until recently I keep hitting quota by making very few request and this started ever since I installed dotnetbrowser library. its the best library for my project because it makes getting data easier. however, I will appreciate if someone can tell me how to get same data without using a browser control like web browser or dotnetbrowser. the data I needed go through multiple server and client communications before the needed value is provided. So my question is how can achieve the same thing without using browser control?

finally, my code might be buggy given that I am not too familiar with threads and task. I might be using too much memory. so below is my code

using DotNetBrowser;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.Http;

namespace AjaxRequest.Controllers
{
public class ValuesController : ApiController
{

    private static ManualResetEvent waitEvent;
    private static List<string> ajaxUrls = new List<string>();
    static string str = "";
    public static Browser browser;


    public ValuesController()
    {


        waitEvent = new ManualResetEvent(false);
        browser = BrowserFactory.Create();
        browser.Context.NetworkService.ResourceHandler = new AjaxResourceHandler();
        browser.Context.NetworkService.NetworkDelegate = new AjaxNetworkDelegate();


    }




    // GET api/values
    public string Get(int id, string title)
    {
        string Title = title.Replace(" ", "-");

        browser.LoadURL(string.Format("https://ba.com/foo/{0}-{1}/something.html", Title, id));
        waitEvent.WaitOne();


        browser.Dispose();
       string Json = Regex.Replace(str, @"\\","");

        return Json.Replace("\\\"", "\"");


    }

    public class AjaxResourceHandler : ResourceHandler
    {
        //HomeController hc;
        public bool CanLoadResource(ResourceParams parameters)
        {
            if (parameters.ResourceType == ResourceType.XHR && parameters.URL.Contains("https://something.com/ajax/blahblah"))
            {

                ajaxUrls.Add(parameters.URL);

            }

            return true;
        }
    }

    public class AjaxNetworkDelegate : DefaultNetworkDelegate
    {
        //HomeController hc;
        public override void OnDataReceived(DataReceivedParams parameters)
        {

            if (ajaxUrls.Contains(parameters.Url))
            {

                PrintResponseData(parameters.Data);

            }


        }
        public void PrintResponseData(byte[] data)
        {


            str = Encoding.UTF8.GetString(data);
            ajaxUrls.Clear();
            browser.Stop();
            browser.dispose();
            waitEvent.Set();

        }
        public void error(string info)
        {


            str = info;


            waitEvent.Set();

        }



    }

}
}

is it possible that I am doing it wrong? if that's the case how can it be improved to conserve memory or data?

UPDATE: am using azure free hosting servicesenter image description here

Cody
  • 353
  • 2
  • 3
  • 17
  • Can you explain what exactly you are trying to do with `DotNetBrowser` in your web app? – Den Feb 25 '17 at 07:13
  • the application communicates with a website, when values are fed the websites am getting my data from goes through a series of Ajax request then the chromium wrapper executes the data via JavaScript then make another Ajax request with the result this goes on till the required data is gotten. I don't thing know httpclient can do this. in my case when I listen to the XHR request... it takes 4 require XHR request before I get the data I want. – Cody Feb 25 '17 at 08:10
  • You're not explaining what your goal is clearly in this statement: **the data I needed go through multiple server and client communications before the needed value is provided** --- we can't help with alternative solutions based on this... – Svek Feb 25 '17 at 14:44
  • I explained what my Goals are in the Original post. I am explaining to Den why I am using dotnetbrowser. I can get all the data I needed with this Method and in my original post I asked if there are any alternative to doing this as well as reduce the memory being used by my code. those are the reasons I posted the question. that statement I made is explaining the reason why I went with dotnetbrowser instead of httpclient. websites for example stackoverflow fires ajax request to update the page on the fly. httpclient cannot get those ajax communication with the server – Cody Feb 25 '17 at 15:38

2 Answers2

0

DotNetBrowser is a Chromium wrapper - I am not entirely sure why you would need it in a web app, but that said, it is likely it is the culprit. Once you remove it, you can use HttpClient to perform the right requests with no memory overhead.

Profiling-wise, your best bet is to start with Application Insights - it's enabled by default in ASP.NET Core projects. It will allow resource tracking across app components.

Den
  • 16,686
  • 4
  • 47
  • 87
  • I am using it for my API. the data I receive cannot be gotten with httpclient since the data comes from Ajax request from multiple source. I don't know if I am explaining it well but for eg. if an Ajax request is made, if could get data which a browser can execute via JavaScript they pass the outcome to another Ajax request till the required value is gotten. which is why I need a browser control. using httpwebrequest or other web client that is not a browser control will just download the dom file and not jquery will be executed to get the needed string or JSON – Cody Feb 25 '17 at 08:01
  • This should be a comment, not an answer, as it's just a suggestion for figuring out memory usage, not a root-cause fix. – David Makogon Feb 25 '17 at 15:56
  • @Cody you can definitely get the data you want through `HttpClient`. Using a Chromium wrapper in a web app is absolutely unnecessary. – Den Feb 25 '17 at 19:01
  • I was expecting something more from you or are you working on it? – Cody Feb 25 '17 at 19:46
  • @Cody and by "expecting more from you" you mean what exactly? – Den Feb 28 '17 at 01:43
  • @Cody you're conflating trolling and the expectation of others doing homework for you. Best of luck with the project. – Den Feb 28 '17 at 18:10
0

It seems like you have more than one running Browser instance.

I can suggest to check that Browser instance is disposed correctly. If not, you can try to dispose it in the Dispose method of the controller.

Eugene Yakush
  • 259
  • 4
  • 6
  • I set it to dispose immediately the data I needed is gotten. but could you explain more or show me how I might be running more than one browser instance. thanks – Cody Feb 27 '17 at 12:59