1

As a simple example, I would like to show all users in a database, and the API provides the async method ShowUsersAsync().

public async Task<InfluxResult<UserRow>> ShowUsersAsync()
/// SHOW all existing users and their admin status.

Now, I am trying to do the following.

class MainClass
{
    public static void Main(string[] args)
    {
        var client = new InfluxClient(new Uri("http://localhost:8086"));

        **//"How do I run it here?"** TestAsync

        Console.WriteLine("PRINT ALL USERS");
    }

    public async void TestAsync(InfluxClient client)
    {
        var users = await client.ShowUsersAsync();
    }
}

Am I missing something about async and await?

API reference: https://github.com/MikaelGRA/InfluxDB.Client

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Inforneer
  • 37
  • 2
  • 6
  • Note that async/await is not good in console applications in the `Main` method. – Tim Jan 26 '17 at 02:30
  • 2
    Possible duplicate of [Can't specify the 'async' modifier on the 'Main' method of a console app](http://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app) – Igor Jan 26 '17 at 02:35
  • 1
    Where do you actually intend on using the api? Because using it in a console application isn't a good example if it is not the intended platform. Also `TestAsync` should return `Task`. – Nkosi Jan 26 '17 at 02:46

2 Answers2

2

According to documentation at the link provided in OP accessing the API should look like this in your intended application.

public async Task<InfluxResult<UserRow>> TestAsync() {
    var client = new InfluxClient(new Uri("http://localhost:8086"));
    var users = await client.ShowUsersAsync();
    return users;
}

Though the simple example in the OP uses a Console Application, the assumption here is that the console application is not the eventual final system to be developed.

But if it is then have a look at this article : Async Console Programs

the above code would be used in your final program as

var users = await TestAsync();

If at this time the intention is just to test the API then Unit Tests may be a good candidate.

[UnitTest]
public class InfluxClientTests {
    [TestMethod]
    public async Task InfluxClient_Should_Get_All_Users() {
        //Arrange        
        var client = new InfluxClient(new Uri("http://localhost:8086"));
        //Act
        var users = await client.ShowUsersAsync();

        //Assert
        //...verify expected results.
    }
}

The test(s) can be run/debugged in the test runner and the behaviors verified with out the limitations of the console application.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

You can do it like so:

TestAsync(client).Wait();
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37