Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. I am having problem creating a script to monitor Rest/API services.

I am having problem creating a script to monitor Rest/API services.

Scheduled Pinned Locked Moved ASP.NET
csharpjsoncomsysadmin
21 Posts 3 Posters 302 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Richard Deeming

    Sorry, missed the return statement:

    static async Task Main(string[] args)
    {
    System.Collections.Concurrent.ConcurrentDictionary urlToStatus = new();

    IEnumerable\> tasks = args.Select(async url =>
    {
        bool result = await ServerStatusByAsync(url);
        urlToStatus.TryAdd(url, result ? "UP" : "DOWN");
    });
    
    bool\[\] results = await Task.WhenAll(tasks);
    
    StringBuilder body = new("Please find the status of the servers below:");
    foreach (var kvp in urlToStatus)
    {
        body.AppendLine();
        body.AppendFormat("{0}: {1}", kvp.Key, kvp.Value);
    }
    
    await SendEmailAsync("Server Status", body.ToString());
    await Task.Delay(3000);
    
    // Return the number of servers which were down:
    return results.Count(result => !result);
    

    }

    Running it is still the same, except you now need to pass a list of URLs to test, rather than a list of server names to ping.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    S Offline
    S Offline
    samflex
    wrote on last edited by
    #21

    Oh man! Getting the following error: Unhandled exception. System.InvalidCastException: Unable to cast object of type 'SelectArrayIterator`2[System.String,System.Threading.Tasks.Task]' to type 'System.Collections.Generic.IEnumerable`1[System.Threading.Tasks.Task`1[System.Boolean]]'. It says line 21 which is this line:

    IEnumerable> tasks = (IEnumerable>)args.Select(async url =>

    This is the second line in Main(). Just for clarity sir, this is your version:

    IEnumerable < Task > tasks = args.Select(async url =>

    However, when I ran it, I got: CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable>'. An explicit conversion exists (are you missing a cast?) When I selected the suggested explicit cast option, the error went away but then I got the other error during testing. The error occurred when I tried passing one URL as an argument to test like: showserverstatus.exe google.com UPDATE: It's always something!!! I got the error worked out; just took me awhile to figure out what was missing - return... Now, email is not working. Not getting sent. UPDATE: Once again, I panicked and didn't calm down to look for solutions. Now, it is working. It turns out that I added myemail.com.net which obviously will make it hard to receive an email. After correcting it, everything is fine now. My Lord, it is impossible to express my most heartfelt gratitude for your patience, incredible ingenuity and most of all, you don't insult me. Thank you very much sir, THANK YOU!!!

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups