Using static async Task in order to get a mac vendor in C# LookupMac(string MacAddress)

0

I'm going to be modifying a script I've found at Rosetta Code which will check MAC addresses against the excellent api at http://api.macvendors.com.

I can do this in Python and Go without any problem but in C# (because it has to be run from a desktop via an Icon) I'm running into problems...not least of all is the fact that the code doesn't make a whole lot of sense to me. I've omitted (from here) the 'Using...' lines

 class Program
{
    static async Task<string> LookupMac(string MacAddress)
    {
        var uri = new Uri("http://api.macvendors.com/" + WebUtility.UrlEncode(MacAddress));
        using (var wc = new HttpClient())
            return await wc.GetStringAsync(uri);
    }
    static void Main(string[] args)
    {
        foreach (var mac in new string[] { "88:53:2E:67:07:BE", "FC:FB:FB:01:FA:21", "D4:F4:6F:C9:EF:8D" })
            Console.WriteLine(mac + "\t" + LookupMac(mac).Result);
        Console.ReadLine();
    }
}

I'm falling over at the whole async side of things! When I try running the code here is the error message from Vis Studio:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at GetMacVend.Program.Main(String[] args) in C:\Users\wildm\VisC#\GetMacVend\GetMacVend\Program.cs:line 24

Inner Exception 1:
TaskCanceledException: A task was canceled.
c#
asynchronous
asked on Stack Overflow Feb 14, 2019 by Mark Wild • edited Feb 14, 2019 by Mikev

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0