ping class 在已发布的网络核心网络应用程序中不起作用

ping class doesn't work in published net core web app

我在 net core web API 控制器中有一个非常简单的 ping 端点。它在本地工作。

[HttpGet("ping")]
    public IActionResult Ping()
    {

        string host = "www.google.com";
        Ping servicePing = new Ping();
        var reply = servicePing.Send(host);

        return Ok($"{reply.Status}");
    }

但是在已发布的 Azure 应用程序上,当我转到该端点时,它 returns 内部服务器错误的 500 状态代码。

我试过使用异步添加一些块包,但它总是一样的——在本地工作,但在发布时不工作。

我也试过检查 Azure 门户,也许他们的服务器阻止了 ping?

编辑:

来自 Azure 门户应用服务诊断报告:

at System.Net.NetworkInformation.Ping.InitialiseIcmpHandle
at System.Net.NetworkInformation.Ping.DoSendPingCore
at System.Net.NetworkInformation.Ping.GetAddressAndSend
at System.Net.NetworkInformation.Ping.GetAddressAndSend
at System.Net.NetworkInformation.Ping.Send
at TestApi.Controllers.WeatherForecastController.Ping
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync

编辑 2:

我检查了 Kudos 中的调试控制台以查找 eventlog.xml 文件以查找异常。 (请告诉我是否有更好的方法来检查服务器异常。)

这是实际的例外情况:

System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
 ---> System.ComponentModel.Win32Exception (5): Access is denied.
   at System.Net.NetworkInformation.Ping.InitialiseIcmpHandle()
   at System.Net.NetworkInformation.Ping.DoSendPingCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options, Boolean isAsync)
   at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   --- End of inner exception stack trace ---
   at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress)
   at TestApi.Controllers.WeatherForecastController.Ping() in C:\Users\Dellas\source\repos\TestApi\TestApi\Controllers\WeatherForecastController.cs:line 48

这里有什么解决方法? Azure 真的会阻止 ping 吗?

是的,在 Azure 应用服务上,由于安全限制,工具 ping、nslookup 和 tracert 将无法通过控制台运行。但是,有两个单独的工具:为了测试 DNS 功能,您可以利用 nameresolver.exe 和 Tcpping - 它允许您测试与主机和端口组合的 TCP 连接。

为了突出更多细节,标准 Azure Web 应用程序 运行 在称为沙箱的安全环境中。每个应用程序 运行 都在自己的沙箱中,将其执行与同一台机器上的其他实例隔离开来,并提供额外的安全性和隐私性,否则将无法获得。 在这种环境下,通过互联网访问应用程序的唯一方法是通过已经公开的 HTTP (80) 和 HTTPS (443) TCP 端口;应用程序可能不会在其他端口上侦听来自 Internet 的数据包。

尝试连接到本地地址(例如 localhost、127.0.0.1)和机器自己的 IP 将失败,除非同一沙箱中的另一个进程在目标端口上创建了侦听套接字。

查看此 article 了解有关此主题的更多详细信息。 从 Kudu 控制台,您可以执行 tcpping www.google.com:80