HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

How to improve execution of web service using System.DirectoryServies.AccountManagement that runs very slow?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
systemslowimproveserviceaccountmanagementusingwebhowthatdirectoryservies

Problem

This method runs in just under two minutes. I would like to optimize it to run in less than 15 seconds. Using a LAMDA filter on my list before iterating it's elements and removing one conditional statement in the method shaved off 30 seconds. Any ideas to improve performance?

c# .net 4.0

```
[SecurityCritical]
[SecurityPermissionAttribute(SecurityAction.Demand)]
private static void GetGroupMembership(List userGroupProperties)
{
List groupProperties = new List();

foreach (ActiveDirectoryPrincipalProperties gProperties in userGroupProperties.FindAll(token => token.groupYesNo.Equals(true)))
{

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, gProperties.groupDomain);
try
{

GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, gProperties.groupName);

foreach (Principal member in group.GetMembers(true))
{
ActiveDirectoryPrincipalProperties memberProperties = new ActiveDirectoryPrincipalProperties();
memberProperties.fullGroupName = gProperties.fullGroupName;
memberProperties.groupDomain = gProperties.groupDomain;
memberProperties.groupName = gProperties.groupName;
memberProperties.groupType = gProperties.groupType;
memberProperties.groupYesNo = false;
memberProperties.memberDomain = member.Context.Name.ToString();
memberProperties.memberName = member.SamAccountName.ToString();
memberProperties.memberType = member.StructuralObjectClass.ToString();
memberProperties.sqlUserOnlyYesNo = false;

groupProperties.Add(memberProperties);
}
group.Dispose();

Solution

If you are writing this in Visual Studio 2010, it would be worth profiling your code to find out exactly what method calls take the longest.

Here is a link where you can watch a video from Channel9 about using the built in performance analyser tool in Visual Studio 2010 to perform CPU Sampling on your code: http://channel9.msdn.com/Blogs/wriju/CPU-Sampling-using-Visual-Studio-2010-Performance-Analyzer-Tool

Once you've identified the methods that take the longest to execute, you can start to work out if you're making any redundant or excessive calls and remove them, or research each call to find ways of optimising each.

Context

StackExchange Code Review Q#3090, answer score: 2

Revisions (0)

No revisions yet.