2017-10-04 5 views
0

ProblèmeComment faire pour empêcher FileNotFoundException dans GetAllNetworkInterfaces() lorsqu'il est appelé dans l'environnement AWS Lambda .net?

La méthode NetworkInterface.GetAllNetworkInterfaces() lance une FileNotFoundException si elle est exécutée dans un environnement Lambda AWS.

Analyse

Cette fonction:

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"; 
     } 
    } 
} 

conduit à cette exception si elle est exécutée dans un environnement Lambda AWS.

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) 

Une idée de quoi faire?

+1

Mettez un essai autour d'elle? – BugFinder

+1

Si le code est correct dans votre contexte, vous voulez lire toutes les interfaces réseau dans le cloud AWS, pour quoi exactement? –

+0

En fait cette ligne de code est incluse dans une bibliothèque tierce que je veux utiliser. Il lance une exception lors de l'initialisation et j'ai été en mesure de retracer la source de l'exception à cet appel exact. Le contexte est que je nous une bibliothèque de haut niveau pour interroger un serveur DNS. –

Répondre