0

I'm pretty new to working with HTTP stuff so I'm rather confused as to what would be the best approach to request data from a HTTP address every few seconds or so.

The API I'm using has - at least to my knowledge no webhook support. So I imagine the way to update my data would be a rather crude way of doing so.

I want this to happen in the background so the GUI does not freeze and become unresponsive. So I know I (probably) need to fiddle with threads.

Best results I've had has been with a async/await Timer. I'm not entirely sure how to work with this and the only way for me to get it to work is to throw an exception after it has elapsed. If I don't - it says that not all nodes return a value and I can't even use return which really, really confuses me.

How should I be doing this?

If it's of any use, I'm working on creating my own RCON tool for a game which has all kinds of server data available via a HTTP API - but documentation for this API is very lackluster.

Dealman
  • 127
  • 1
  • 2
  • 9
  • are you using .net core or .net framework? – Roelant M Nov 03 '17 at 08:22
  • .NET Framework :) – Dealman Nov 03 '17 at 08:23
  • Framework or Core, the answer is the same. You can easily call the service in a loop with an `await Task.Delay()` call. HttpClient's methods are asynchronous too – Panagiotis Kanavos Nov 03 '17 at 08:49
  • So I can use a while loop and Task.Delay() to prevent it from freezing the program? Wouldn't this spam the connection with requests, though? Or does the await solve that by - quite literally, awaiting for the request to finish? I'm still pretty new to the concept of await/async as I hail originally from Lua so this is all a bit alien to me. If so, I should just be able to use stopwatch to kind of control the intervals at which it requests data, right? – Dealman Nov 03 '17 at 08:59

1 Answers1

0

if you go to .net core you can see my previous answer on: Start multiple background threads inside Self Hosted ASP.NET Core Microservice

for .net framework you have to do a little more yourself. But still very do-able!

in your global.asax you have (or should I say: should) have your dependency injection. Something like:

protected void Application_Start()
{
    Bootstrap();

    //and do something more
}

private static void Bootstrap()
{
    var container = new Container();
    container.Register(() => new HttpClient());

    container.RegisterSingleton<IApiCaller, ApiCaller>();
    container.RegisterInitializer<IApiCaller>(ApiCaller=> apicaller.StartCallingAsync());

    // Suppress warnings for HttpClient
    var registration = container.GetRegistration(typeof(HttpClient)).Registration;
    registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "Dispose is being called by code.");
    registration.SuppressDiagnosticWarning(DiagnosticType.LifestyleMismatch, "Every HttpCient is unique for each dependency.");

    container.Verify();

    GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
}

In this case, I let SimpleInjector start my background thread to do a lot of work. In the apicaller you can do your httpcalls.

something like:

public async Task StartCallingAsync(CancellationToken cancellationToken = (default)CancellationToken)
{
    while(true)
    {
       var response = await _httpClient.GetAsync(url);
       if (response.IsSuccessStatusCode)
       {
           //do work
       }
       await Task.Delay(10000, cancellationToken);
    }
}

for the GetAsync there are extension methods that can cast it directly to your object.

can you work with this?

Roelant M
  • 1,581
  • 1
  • 13
  • 28
  • That is not .Net Core but ASP.NET Core which works with .Net Core or .Net Framework – Sir Rufo Nov 03 '17 at 08:48
  • You don't need dependency injection to call `HttpClient` in a loop. You gain nothing either. You *lose* the ability to specify HttpHandlers. And the OP clearly talks about the UI thread, so this isn't an ASP.NET application – Panagiotis Kanavos Nov 03 '17 at 08:48
  • offcourse you don't need DI, but it makes life easier. But you got me on the UI thread. But offcourse, in a UI -application you can still do DI, and thus, you can implement it everywhere :) – Roelant M Nov 03 '17 at 09:36
  • In the end this *doesn't* answer the question, only shows DI configuration code. Where's the polling loop? – Panagiotis Kanavos Nov 03 '17 at 12:53