在AWS Lambda .net环境中调用时如何防止GetAllNetworkInterfaces()中的FileNotFoundException?

时间:2017-10-04 14:49:43

标签: c# amazon-web-services lambda

问题

如果在AWS Lambda环境中执行,方法NetworkInterface.GetAllNetworkInterfaces()会抛出FileNotFoundException

分析

此功能:

using System.Net.NetworkInformation;

[assembly: Amazon.Lambda.Core.LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace CheckerLambda
{
    public class Function
    {
        public string FunctionHandler(Amazon.Lambda.Core.ILambdaContext context)
        {
            // Here we get an "System.IO.FileNotFoundException: Could not find file '/sys/class/net/lo/mtu'."
            var adapters = NetworkInterface.GetAllNetworkInterfaces();

            // do something with 'adapters'

            return "This point is never reached";
        }
    }
}

如果在AWS Lambda环境中执行,则导致此异常。

Unhandled Exception: System.IO.FileNotFoundException: Could not find file '/sys/class/net/lo/mtu'.
2017-47-04 15:47:21:    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
2017-47-04 15:47:21:    at Interop.CheckIo[TSafeHandle](TSafeHandle handle, String path, Boolean isDirectory, Func`2 errorRewriter)
2017-47-04 15:47:21:    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
2017-47-04 15:47:21:    at System.IO.UnixFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
2017-47-04 15:47:21:    at System.IO.UnixFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
2017-47-04 15:47:21:    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
2017-47-04 15:47:21:    at System.IO.File.InternalReadAllText(String path, Encoding encoding)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.StringParsingHelpers.ParseRawIntFile(String filePath)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.LinuxIPv4InterfaceProperties..ctor(LinuxNetworkInterface linuxNetworkInterface)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.LinuxIPInterfaceProperties..ctor(LinuxNetworkInterface lni)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.LinuxNetworkInterface..ctor(String name)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.LinuxNetworkInterface.GetOrCreate(Dictionary`2 interfaces, String name)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.LinuxNetworkInterface.<>c__DisplayClass5_0.<GetLinuxNetworkInterfaces>b__2(String name, LinkLayerAddressInfo* llAddr)
2017-47-04 15:47:21:    at Interop.Sys.EnumerateInterfaceAddresses(IPv4AddressDiscoveredCallback ipv4Found, IPv6AddressDiscoveredCallback ipv6Found, LinkLayerAddressDiscoveredCallback linkLayerFound)
2017-47-04 15:47:21:    at System.Net.NetworkInformation.LinuxNetworkInterface.GetLinuxNetworkInterfaces()
2017-47-04 15:47:21:    at CheckerLambda.Function.FunctionHandler(ILambdaContext context)
2017-47-04 15:47:21:    at lambda_method(Closure , Stream , Stream , ContextInfo )

知道该怎么做?

1 个答案:

答案 0 :(得分:0)

原来这是一个已知错误,将通过.net core 2.0修复:https://github.com/dotnet/corefx/issues/24422#issuecomment-334190332