2011-03-21 46 views
4

我花了一天多地发现,主要目的是使用方式更多的带宽比使用的DirectoryServices。情景就是这样。我有一个组里面有3000个计算机对象。要检查计算机是否该组中我检索到的GroupPrincipal和搜索的ComputerPrincipal。网络利用率 - AccountManagement对比的DirectoryServices

 Boolean _retVal = false; 
     PrincipalContext _principalContext = null; 
     using (_principalContext = new PrincipalContext(ContextType.Domain, domainController, srv_user, srv_password)) { 
      ComputerPrincipal _computer = ComputerPrincipal.FindByIdentity(_principalContext, accountName); 

      GroupPrincipal _grp = GroupPrincipal.FindByIdentity(_principalContext, groupName); 
      if (_computer != null && _grp != null) { 
       // get the members 
       PrincipalSearchResult<Principal> _allGrps = _grp.GetMembers(false); 
       if (_allGrps.Contains(_computer)) { 
        _retVal = true; 
       } 
       else { 
        _retVal = false; 
       } 
      } 
     } 
     return _retVal; 

其实非常漂亮的界面,但是这会为每个请求创建大约12MB的流量。如果您的域控制器位于局域网中,则不存在问题。如果您使用WAN访问域控制器,则会终止连接/应用程序。

我注意到了这一点后,我使用的DirectoryServices

Boolean _retVal = false; 
DirectoryContext _ctx = null; 
try { 
    _ctx = new DirectoryContext(DirectoryContextType.DirectoryServer, domainController, srv_user, srv_password); 
} catch (Exception ex) { 
    // do something useful 
} 
if (_ctx != null) { 
    try { 
     using (DomainController _dc = DomainController.GetDomainController(_ctx)) { 
      using (DirectorySearcher _search = _dc.GetDirectorySearcher()) { 
       String _groupToSearchFor = String.Format("CN={0},", groupName); 
       _search.PropertiesToLoad.Clear(); 
       _search.PropertiesToLoad.Add("memberOf"); 
       _search.Filter = String.Format("(&(objectCategory=computer)(name={0}))", accountName); ; 
       SearchResult _one = null; 
       _one = _search.FindOne(); 
       if (_one != null) { 
        int _count = _one.Properties["memberOf"].Count; 
        for (int i = 0; i < _count; i++) { 
         string _m = (_one.Properties["memberOf"][i] as string); 
         if (_m.Contains(groupName)) { _retVal = true; } 
        } 
       } 
      } 
     } 
    } catch (Exception ex) { 
     // do something useful 
    } 
} 
return _retVal; 

此实现将使用网络流量12K左右重新实现相同的功能。这可能不是很好,但可以节省很多带宽。

我的问题是现在如果有人有一个想法是什么AccountManagement对象是做它使用这么多带宽?

谢谢!

+0

我正在开发与AccountManagement接口的东西,我很乐意更多地了解这个... – Emmanuel 2011-03-21 16:06:59

+0

我很高兴为您节省一些时间:-) – auhorn 2011-03-22 12:25:58

回答

2

我猜想包括以下行会做大量节省带宽:

_search.PropertiesToLoad.Clear(); 
_search.PropertiesToLoad.Add("memberOf"); 
_search.Filter = String.Format("(&(objectCategory=computer)(name={0}))", accountName); 

前两个是在告诉的DirectorySearcher只加载的,而不是谁知道一个单一的财产有多少任意大小的。

第二个是将一个过滤器传递到DirectorySearcher,我猜可能是处理服务器端,进一步限制了结果集的大小。

+0

在第二个示例中,我只是加载所需的示例。这个版本也只是产生约12k的流量。第一个版本更清洁和更好的版本创造更多的交易。我的回答 – auhorn 2011-03-22 12:25:29

+0

去掉一部分,我会搞混了。 – tomfanning 2011-03-23 21:23:54

+0

确定 - 不过这是我在这是对网络利用率ok了第二个例子我做。我的问题是关于第一个例子。 – auhorn 2011-03-24 11:59:31