/// <summary>
    /// Asynchronously returns a collection of IPHostEntries of all computers found in the NetworkNeighborhood
    /// </summary>
    /// <returns></returns>
    private async Task<ICollection<IPHostEntry>> DoGetHostsAsync()
    {
      var stopWatch = Stopwatch.StartNew();

      // Enumerate all computers in the NetworkNeighborhood for all Domains / Workgroups
      var hosts = new Dictionary<String, Task<IPHostEntry>>();
      try
      {
        using (var root = new DirectoryEntry(PROVIDER_STRING))
        {
          foreach (DirectoryEntry domain in root.Children)
          {
            using (domain)
            {
              foreach (DirectoryEntry computer in domain.Children)
              {
                using (computer)
                {
                  if (computer.SchemaClassName == COMPUTER_OBJECT)
                  {
                    ServiceRegistration.Get<ILogger>().Info("DirectoryEntryNeighborhoodBrowser: Adding '{0}' to hosts list.", computer.Name);
                    hosts[computer.Name] = Dns.GetHostEntryAsync(computer.Name);
                  }
                }
              }
            }
          }
        }
      }
      catch (Exception e)
      {
        ServiceRegistration.Get<ILogger>().Error("DirectoryEntryNeighborhoodBrowser: Error while enumerating computers in the NetworkNeighborhood", e);
      }

      // Wait until an IPHostEntry was found for all computers
      try
      {
        await Task.WhenAll(hosts.Values);
      }
      catch (System.Net.Sockets.SocketException) { }
      catch (Exception e)
      {
        ServiceRegistration.Get<ILogger>().Error("DirectoryEntryNeighborhoodBrowser: Error while getting IPHostEntries", e);
      }

      // Assemble the result
      var result = new HashSet<IPHostEntry>();
      foreach (var kvp in hosts)
      {
        // If the call to GetHostEntryAsync was successful, we return the retrieved IPHostEntry
        // If not, we return a new IPHostEntry object just with its HostName property set.
        var host = kvp.Value.IsFaulted ? new IPHostEntry { HostName = kvp.Key } : kvp.Value.Result;
        NeighborhoodBrowserHelper.ConvertDnsHostNameToNetBiosName(host);
        result.Add(host);
      }
      NeighborhoodBrowserHelper.LogResult(result, GetType().Name, stopWatch.ElapsedMilliseconds);
      return result;
    }
示例#2
0
        /// <summary>
        /// Asynchronously returns a collection of IPHostEntries of all computers found in the NetworkNeighborhood
        /// </summary>
        /// <returns></returns>
        private async Task <ICollection <IPHostEntry> > DoGetHostsAsync()
        {
            var stopWatch = Stopwatch.StartNew();

            // Enumerate all computers in the NetworkNeighborhood for all Domains / Workgroups
            var hosts = new Dictionary <String, Task <IPHostEntry> >();
            ICollection <String> hostNames;

            // The WNetEnum API requires at least to be impersonated as NetworkService; this is the fallback credential used for
            // the root path of NetworkNeighborhoodResourceProvider if no other credentials have been entered.
            using (ServiceRegistration.Get <IImpersonationService>().CheckImpersonationFor(NetworkNeighborhoodResourceProvider.RootPath))
                hostNames = NetworkResourcesEnumerator.EnumerateResources(ResourceScope.GlobalNet, ResourceType.Disk, ResourceUsage.All, ResourceDisplayType.Server);

            // The hostNames returned by the NetworkResourceEnumerator are in the form \\COMPUTERNAME
            // We have to remove the leading \\ to be able to pass the hostName to GetHostEntryAsync
            foreach (var hostName in hostNames)
            {
                hosts[hostName.Substring(2)] = Dns.GetHostEntryAsync(hostName.Substring(2));
            }

            // Wait until an IPHostEntry was found for all computers
            try
            {
                await Task.WhenAll(hosts.Values);
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Error("WNetEnumNeighborhoodBrowser: Error while getting IPHostEntries", e.Message);
            }

            // Assemble the result
            var result = new HashSet <IPHostEntry>();

            foreach (var kvp in hosts)
            {
                // If the call to GetHostEntryAsync was successful, we return the retrieved IPHostEntry
                // If not, we return a new IPHostEntry object just with its HostName property set.
                var host = kvp.Value.IsFaulted ? new IPHostEntry {
                    HostName = kvp.Key
                } : kvp.Value.Result;
                NeighborhoodBrowserHelper.ConvertDnsHostNameToNetBiosName(host);
                result.Add(host);
            }
            NeighborhoodBrowserHelper.LogResult(result, GetType().Name, stopWatch.ElapsedMilliseconds);
            return(result);
        }