我在OS X上的DNX Core 5.0中遇到SendPingAsync问题。基本上我正在尝试构建一个基于MVC的小站点,该站点ping一系列IP地址10.0.0.1 - > 10.0.0.254。我到目前为止的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using System.Net;
using System.Net.NetworkInformation;
namespace PingTest1.Controllers
{
public class HomeController : Controller
{
public static async Task<PingReply> sendPing(IPAddress ip)
{
var reply = await new Ping().SendPingAsync(ip, 1000);
return reply;
}
public IActionResult Index()
{
Dictionary<string, string> ipas = new Dictionary<string, string>();
for (int i = 1; i < 255; i++) {
IPAddress ipa = new IPAddress(new byte[] {10, 0, 0, (byte)i});
Task<PingReply> result = sendPing(ipa);
ipas.Add("10.0.0." + i, result.Result.Status.ToString());
}
ViewData["ipas"] = ipas;
return View();
}
public IActionResult Error()
{
return View();
}
}
}
因以下未被捕获的例外而失败:
An unhandled exception has occurred: Method not found: 'System.Threading.Tasks.Task`1<System.Net.NetworkInformation.PingReply> System.Net.NetworkInformation.Ping.SendPingAsync(System.Net.IPAddress, Int32)'.
System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task`1<System.Net.NetworkInformation.PingReply> System.Net.NetworkInformation.Ping.SendPingAsync(System.Net.IPAddress, Int32)'.
我已将完整的源代码上传到GitHub:https://github.com/00101010b/PingTest1
作为C#和ASP.net 5.0的完整菜鸟,我不知道我在做什么,所以任何帮助都会非常感激。