我在控制台应用程序和webapi应用程序中都创建了一个ping方法。 Ping适用于控制台应用程序,但不适用于webapi应用程序。它总是超时的。这是我正在使用的代码:
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;
}
}
}
有什么想法吗?