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

问题描述 投票:1回答:1

我在网络核心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应用程序上,当我转到该终结点时,它将为内部服务器错误返回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 api .net-core ping
1个回答
0
投票

是的,由于安全性限制,在Azure App Service上,ping,nslookup和tracert工具无法在控制台中使用。但是,有两个单独的工具:为了测试DNS功能,您可以利用nameresolver.exe和Tcpping-这允许您测试与主机和端口组合的TCP连接。

为了突出显示更多详细信息,标准Azure Web Apps在称为沙盒的安全环境中运行。每个应用程序都在其自己的沙箱中运行,从而将其执行与同一台计算机上的其他实例隔离开来,并提供其他程度的安全性和隐私性,否则这些安全性和隐私性将是不可用的。在这种环境中,可以通过Internet访问应用程序的唯一方法是通过已经公开的HTTP(80)和HTTPS(443)TCP端口;而在这种情况下,可以通过Internet访问应用程序。应用程序可能不会在其他端口上侦听来自Internet的数据包。

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

请参阅article,以获取有关此主题的更多详细信息。在Kudu控制台中,您可以执行tcpping www.google.com:80

© www.soinside.com 2019 - 2024. All rights reserved.