最近在做一个MS的项目,我负责一个检索AD中的用户和组数据信息,这里把她作为一个例子给大家分享。
首先从AD中查询并构造一个对象池,查询检索AD数据是非常耗费时间的,为了不至于让UI没有响应,我采用了使用后台线程来运行AD检索。下面是查询AD中所有组使用的代码:(顺便说一下,DirectorySearcher和SearchResultCollection是System.DirectoryServices下的成员,在使用前要把System.DirectoryServices.dll添加引用项目中)
这个查询在执行时是非常耗费时间的,由于查询并不是非常频繁的更改,所以我这里使用一个缓冲池来保存这些查询结果。
由于缓冲池最后会被异步的访问,可能会有多个线程同时访问她,不得不使用其他来保证这个缓冲池状态不被破坏。我采用创建一个锁来确保同一时间只能有一个线程访问。
private static Object m_groupPoolSync = new Object();
当向缓存池读写数据的时候会对groupPoolSync 对象的加锁。同时会有个全局标识来标识这个池是否已经初始化,这样如果已经有了需要的数据就可以跳过对AD的检索。
有时必须把缓存池清空,这里使用一个统一的方法来做这个,这里方法里也对m_groupPoolSync进行了加锁,因为有可能同时有另外一个线程调用GetAdGroups()方法。
首先从AD中查询并构造一个对象池,查询检索AD数据是非常耗费时间的,为了不至于让UI没有响应,我采用了使用后台线程来运行AD检索。下面是查询AD中所有组使用的代码:(顺便说一下,DirectorySearcher和SearchResultCollection是System.DirectoryServices下的成员,在使用前要把System.DirectoryServices.dll添加引用项目中)
1
private static String[] GetAdGroupsFromAD()
2
{
3
DirectorySearcher srch = new DirectorySearcher();
4![]()
5
const string query = "(&(objectCategory=group))";
6![]()
7
srch.Filter = query;
8![]()
9
SearchResultCollection sResult = srch.FindAll();
10![]()
11
List<String> results = new List<string>();
12![]()
13
if (null != sResult)
14
{
15
for (int i = 0; i < sResult.Count; i++)
16
{
17
if (null != sResult[i].Properties["name"] && sResult[i].Properties["name"].Count == 1)
18
{
19
results.Add(sResult[i].Properties["name"][0] as String);
20
}
21
}
22
}
23
return results.ToArray();
24
}
private static String[] GetAdGroupsFromAD()2
{3
DirectorySearcher srch = new DirectorySearcher();4

5
const string query = "(&(objectCategory=group))";6

7
srch.Filter = query;8

9
SearchResultCollection sResult = srch.FindAll();10

11
List<String> results = new List<string>();12

13
if (null != sResult)14
{15
for (int i = 0; i < sResult.Count; i++)16
{17
if (null != sResult[i].Properties["name"] && sResult[i].Properties["name"].Count == 1)18
{19
results.Add(sResult[i].Properties["name"][0] as String);20
}21
}22
}23
return results.ToArray();24
}这个查询在执行时是非常耗费时间的,由于查询并不是非常频繁的更改,所以我这里使用一个缓冲池来保存这些查询结果。
由于缓冲池最后会被异步的访问,可能会有多个线程同时访问她,不得不使用其他来保证这个缓冲池状态不被破坏。我采用创建一个锁来确保同一时间只能有一个线程访问。
private static Object m_groupPoolSync = new Object();
当向缓存池读写数据的时候会对groupPoolSync 对象的加锁。同时会有个全局标识来标识这个池是否已经初始化,这样如果已经有了需要的数据就可以跳过对AD的检索。
1
private static String[] GetAdGroups()
2
{
3![]()
4
List<String> results = new List<string>();
5![]()
6
lock (m_groupPoolSync)
7
{
8
if (!m_groupPoolInitialized)
9
{
10
foreach (String result in GetAdGroupsFromAD())
11
{
12
m_groupPool.Add(result);
13
}
14![]()
15
m_groupPoolInitialized = true;
16
}
17![]()
18
results.AddRange(m_groupPool);
19
}
20![]()
21
return results.ToArray();
22
}
private static String[] GetAdGroups()2
{3

4
List<String> results = new List<string>();5

6
lock (m_groupPoolSync)7
{8
if (!m_groupPoolInitialized)9
{10
foreach (String result in GetAdGroupsFromAD())11
{12
m_groupPool.Add(result);13
}14

15
m_groupPoolInitialized = true;16
}17

18
results.AddRange(m_groupPool);19
}20

21
return results.ToArray();22
} 有时必须把缓存池清空,这里使用一个统一的方法来做这个,这里方法里也对m_groupPoolSync进行了加锁,因为有可能同时有另外一个线程调用GetAdGroups()方法。
1
public static void ClearPools()
2
{
3
lock (m_groupPoolSync)
4
{
5
m_groupPoolInitialized = false;
6
}
7
}
public static void ClearPools()2
{3
lock (m_groupPoolSync)4
{5
m_groupPoolInitialized = false;6
}7
}

浙公网安备 33010602011771号