Slow ActiveDirectory responses

S

Susanne Christe

Hi all,

I have a Problem with this code, I am using Threads in the main form but
to load all the users from one OU (there are about 700 of them, not
realy very much) it needs about 10-20 sek ( without Threads in the main
form even 1 min).

Do you know how to speed up the searching process?


Thx
Susan

Code: Ho do I get the DataTable full faster?

-------------snip-------------
using System;
using System.DirectoryServices;

using ActiveDs;
using System.Data;

using System.Windows.Forms;

namespace com.pironet.asp.user
{
/// <summary>
///
/// </summary>
public class UserDataSet
{
private static DataSet userDataSet;
private static DataTable userDataTable;

//Root to search from.
private static string dom = ADHelper.GetLDAPDomain();

private static int counter = 0;

static string searchFilter =
("(&(objectCategory=Person)(objectClass=user))");

/// <summary>
/// Empty
/// </summary>
public UserDataSet()
{

}

/// <summary>
/// Creates the Table and fills the Objects
/// </summary>
static UserDataSet()
{
counter = 0;

userDataTable = new DataTable("users");
userDataSet = new DataSet("users");

CreateTable();
PopulateTable();
}

public static DataSet DataSet
{
get{ return userDataSet ;}
set{ userDataSet = value;}
}

public static DataTable DataTable
{
get{ return userDataTable ;}
set{ userDataTable = value;}
}




private static void CreateTable()
{
//Create Columns for DataTable.
userDataTable.Columns.Add("Company",
System.Type.GetType("System.String"));
userDataTable.Columns.Add("Office",
System.Type.GetType("System.String"));

userDataTable.Columns.Add("Vorname",System.Type.GetType("System.String"));

userDataTable.Columns.Add("Nachname",System.Type.GetType("System.String"));
userDataTable.Columns.Add("Name",System.Type.GetType("System.String"));

userDataTable.Columns.Add("LastLogin",System.Type.GetType("System.String"));
userDataTable.Columns.Add("CN",System.Type.GetType("System.String"));

userDataTable.Columns.Add("LastLoginTs",System.Type.GetType("System.String"));
}







private static void PopulateTable()
{

DirectoryEntry entry = new DirectoryEntry( dom, null, null,
AuthenticationTypes.Secure);
CManagerMain.cManagerMain.progressBar1.Value = 1;
GetChildren2(entry);
userDataSet.Tables.Add(userDataTable);
CManagerMain.cManagerMain.progressBar1.Value = 1;

//MessageBox.Show("Treffer * :" + counter);
}








private static void GetChildren2(DirectoryEntry dirEntry)
{

DirectorySearcher src = new DirectorySearcher(dirEntry);

//src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name=" +
sRegEx + "))");
//src.Filter= ("(&(objectCategory=Person)(objectClass=user))");

src.Filter= searchFilter;
src.SearchRoot = dirEntry;

foreach( SearchResult res in src.FindAll() )
{
DirectoryEntry child= res.GetDirectoryEntry();

DataRow topRow = userDataTable.NewRow();

topRow = FillDataRow( topRow, child.Properties["company"], "Company" );
topRow = FillDataRow( topRow,
child.Properties["physicalDeliveryOfficeName"], "Office" );
topRow = FillDataRow( topRow, child.Properties["givenName"],
"Vorname" );
topRow = FillDataRow( topRow, child.Properties["sn"], "Nachname" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLogin" );
topRow = FillDataRow( topRow, child.Properties["sAMAccountName"],
"Name" );

topRow = FillDataRow( topRow, child.Properties["distinguishedName"],
"CN" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLoginTs" );

userDataTable.Rows.Add(topRow);

CManagerMain.cManagerMain.progressBar1.PerformStep();
//counter++;
}
}




public static void SetSearchFilter( String category, String regEx)
{
if( category == null || regEx == null)
{
searchFilter = ("(&(objectCategory=Person)(objectClass=user))");
} else {
searchFilter = ("(&(objectCategory=Person)(objectClass=user)(" +
category + "=" + regEx + "))");
}
}


private static bool Search( DirectoryEntry dirEntry, String category,
String sRegEx )
{
bool treffer = false;

DirectorySearcher src = new DirectorySearcher(dirEntry);
//string sRegEx = "10020047*";

src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name=" +
sRegEx + "))");
src.SearchRoot = dirEntry;

int count = src.FindAll().Count;

if(count > 0) treffer=true;
return treffer;
}

private static DataRow FillDataRow( DataRow dataRow,
PropertyValueCollection property , string columnName )
{

if( property.Value != null )
{
if( columnName.Equals("LastLogin") )
{
dataRow[columnName] = GetDate(property);
}
else if( columnName.Equals("LastLoginTs") )
{
dataRow[columnName] = GetDateTs(property);
}
else
{
dataRow[columnName] = property[0].ToString();
}
}
else
{
dataRow[columnName] = "";
}
return dataRow;
}

private static string GetDate( PropertyValueCollection property )
{
string date = "no login yet";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference
to ILargeInteger
Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;

if( liTicks > 0 && DateTime.MaxValue.Ticks >= liTicks &&
DateTime.MinValue.Ticks <= liTicks)
{
DateTime dTemp = DateTime.FromFileTime(liTicks);
date = dTemp.ToShortDateString()+ " " + dTemp.ToShortTimeString();
}

return date;
}


private static string GetDateTs( PropertyValueCollection property )
{
string ts = "0";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference
to ILargeInteger

Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;
ts = liTicks.ToString();

return ts;
}

}
}


-------------snap--------------
 
W

Willy Denoyette [MVP]

You have to find out how much time it takes to fill the
PropertyValueCollection (see src.FindAll() ).
If this take 10-20 seconds there is only one thing you can do in your code
to speed up the process, that is restrict the properties transferred to
those you really need by setting the PropertiesToLoad like this:

string[] props = {"lastLogon", "physicalDeliveryOfficeName", "givenName",
"sn", "sAMAccountName", "distinguishedname", "company"};
src.PropertiesToLoad.AddRange(props);

, if that doesn't change much, you will need to check your network
connection and/or AD server load to find the bottleneck.

However, when FindAll() takes less than a few seconds, you need post a
complete (short) working repro.

Willy.

Susanne Christe said:
Hi all,

I have a Problem with this code, I am using Threads in the main form but
to load all the users from one OU (there are about 700 of them, not realy
very much) it needs about 10-20 sek ( without Threads in the main form
even 1 min).

Do you know how to speed up the searching process?


Thx
Susan

Code: Ho do I get the DataTable full faster?

-------------snip-------------
using System;
using System.DirectoryServices;

using ActiveDs;
using System.Data;

using System.Windows.Forms;

namespace com.pironet.asp.user
{
/// <summary>
///
/// </summary>
public class UserDataSet
{
private static DataSet userDataSet;
private static DataTable userDataTable;

//Root to search from.
private static string dom = ADHelper.GetLDAPDomain();

private static int counter = 0;

static string searchFilter =
("(&(objectCategory=Person)(objectClass=user))");

/// <summary>
/// Empty
/// </summary>
public UserDataSet()
{

}

/// <summary>
/// Creates the Table and fills the Objects
/// </summary>
static UserDataSet()
{
counter = 0;

userDataTable = new DataTable("users");
userDataSet = new DataSet("users");

CreateTable();
PopulateTable();
}

public static DataSet DataSet
{
get{ return userDataSet ;}
set{ userDataSet = value;}
}

public static DataTable DataTable
{
get{ return userDataTable ;}
set{ userDataTable = value;}
}




private static void CreateTable()
{
//Create Columns for DataTable.
userDataTable.Columns.Add("Company",
System.Type.GetType("System.String"));
userDataTable.Columns.Add("Office", System.Type.GetType("System.String"));

userDataTable.Columns.Add("Vorname",System.Type.GetType("System.String"));

userDataTable.Columns.Add("Nachname",System.Type.GetType("System.String"));
userDataTable.Columns.Add("Name",System.Type.GetType("System.String"));

userDataTable.Columns.Add("LastLogin",System.Type.GetType("System.String"));
userDataTable.Columns.Add("CN",System.Type.GetType("System.String"));

userDataTable.Columns.Add("LastLoginTs",System.Type.GetType("System.String"));
}







private static void PopulateTable()
{
DirectoryEntry entry = new DirectoryEntry( dom, null, null,
AuthenticationTypes.Secure);
CManagerMain.cManagerMain.progressBar1.Value = 1;
GetChildren2(entry);
userDataSet.Tables.Add(userDataTable);
CManagerMain.cManagerMain.progressBar1.Value = 1;

//MessageBox.Show("Treffer * :" + counter);
}








private static void GetChildren2(DirectoryEntry dirEntry)
{

DirectorySearcher src = new DirectorySearcher(dirEntry);
//src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name=" +
sRegEx + "))");
//src.Filter= ("(&(objectCategory=Person)(objectClass=user))");

src.Filter= searchFilter;
src.SearchRoot = dirEntry;

foreach( SearchResult res in src.FindAll() )
{
DirectoryEntry child= res.GetDirectoryEntry();

DataRow topRow = userDataTable.NewRow();

topRow = FillDataRow( topRow, child.Properties["company"], "Company" );
topRow = FillDataRow( topRow,
child.Properties["physicalDeliveryOfficeName"], "Office" );
topRow = FillDataRow( topRow, child.Properties["givenName"], "Vorname" );
topRow = FillDataRow( topRow, child.Properties["sn"], "Nachname" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLogin" );
topRow = FillDataRow( topRow, child.Properties["sAMAccountName"],
"Name" );

topRow = FillDataRow( topRow, child.Properties["distinguishedName"],
"CN" );
topRow = FillDataRow( topRow, child.Properties["lastLogon"],
"LastLoginTs" );

userDataTable.Rows.Add(topRow);

CManagerMain.cManagerMain.progressBar1.PerformStep();
//counter++;
}
}




public static void SetSearchFilter( String category, String regEx)
{
if( category == null || regEx == null)
{
searchFilter = ("(&(objectCategory=Person)(objectClass=user))");
} else {
searchFilter = ("(&(objectCategory=Person)(objectClass=user)(" + category
+ "=" + regEx + "))");
}
}


private static bool Search( DirectoryEntry dirEntry, String category,
String sRegEx )
{
bool treffer = false;

DirectorySearcher src = new DirectorySearcher(dirEntry); //string sRegEx =
"10020047*";

src.Filter= ("(&(objectCategory=Person)(objectClass=user)(name=" + sRegEx
+ "))");
src.SearchRoot = dirEntry;

int count = src.FindAll().Count;

if(count > 0) treffer=true;
return treffer;
}

private static DataRow FillDataRow( DataRow dataRow,
PropertyValueCollection property , string columnName )
{

if( property.Value != null )
{
if( columnName.Equals("LastLogin") )
{
dataRow[columnName] = GetDate(property);
}
else if( columnName.Equals("LastLoginTs") )
{
dataRow[columnName] = GetDateTs(property);
}
else
{
dataRow[columnName] = property[0].ToString();
}
}
else
{
dataRow[columnName] = "";
}
return dataRow;
}

private static string GetDate( PropertyValueCollection property )
{
string date = "no login yet";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference to
ILargeInteger
Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;

if( liTicks > 0 && DateTime.MaxValue.Ticks >= liTicks &&
DateTime.MinValue.Ticks <= liTicks)
{
DateTime dTemp = DateTime.FromFileTime(liTicks);
date = dTemp.ToShortDateString()+ " " + dTemp.ToShortTimeString();
}

return date;
}


private static string GetDateTs( PropertyValueCollection property )
{
string ts = "0";

LargeInteger oli = (LargeInteger) property[0]; //Set object reference to
ILargeInteger

Int64 liTicks = oli.HighPart * 0x100000000 + oli.LowPart;
ts = liTicks.ToString();

return ts;
}

}
}


-------------snap--------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top