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.
Mikev
  • 2,012
  • 1
  • 15
  • 27
Mark Wild
  • 1
  • 2
  • 1
    Possible duplicate of [Can't specify the 'async' modifier on the 'Main' method of a console app](https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app) – Liam Feb 14 '19 at 16:14
  • You need a make an `async` method (for your loop) then call that using `.GetAwaiter().GetResult()` in your `Main`. – Liam Feb 14 '19 at 16:16
  • 2
    Code works for me, except for an occasional rate limit error from the server. – Charles Mager Feb 14 '19 at 16:17
  • Does the TaskCancledException have an InnerException? – Hyarus Feb 14 '19 at 16:20
  • 1
    This isnot the question, but if you have to request many macs later, you will have to use a singleton httpClient. Using a new httpClient as this could give deadlock issues. – LeBigCat Feb 14 '19 at 16:39
  • 1
    You'll get a TaskCanceledException from HttpClient if the request times out. It looks like that might be what's happening here? It's hard to tell without the full stack trace, but it's the most likely cause. Especially if you're getting rate-limited, as @CharlesMager mentioned. – canton7 Feb 14 '19 at 16:51
  • 2
    side note: you shouldn't use `.Result` - or (@Laim) `.GetAwaiter().GetResult()`. If you have a recent C# compiler, the appropriate way to use `async` with `Main()` is: `public async Task Main(string[] args) { ... await LookupMac(mac) ... }` – Marc Gravell Feb 14 '19 at 17:03
  • I did nearly put this @MarcGravell, It was covered in the dupe though so I wanted a short pithy comment – Liam Feb 15 '19 at 09:25

0 Answers0