Home Ping does not work in webApi
Reply: 0

Ping does not work in webApi

Luke101
1#
Luke101 Published in 2017-12-07 16:00:33Z

I have created a ping method in both a console application and webapi application. Ping works the console app but not the webapi app. It always is timedout. Here is the code I am using:

namespace Ping.Controllers
{
    [Route("api/[controller]")]
    public class PingController : Controller
    {
        // GET api/values
        [HttpPost]
        public async Task<IEnumerable<Models.PingItem>> Post([FromBody]List<string> hosts)
        {
            List<Task<Models.PingItem>> tasks = new List<Task<Models.PingItem>>();

            foreach (var host in hosts)
            {
                tasks.Add(Ping(host));
            }

            var results = await Task.WhenAll(tasks.ToArray());

            return results;
        }

        private async Task<Models.PingItem> Ping(string host)
        {
            Models.PingItem pi = new Models.PingItem();
            Ping pingSender = new Ping();
            var result = await pingSender.SendPingAsync(host, 50000);
            pi.Host = host;
            pi.Live = result.Status == IPStatus.Success ? true : false;
            pi.Time = result.RoundtripTime;
            return pi;
        }
    }
}

Any ideas?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.332953 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO