I've been running into an error on one of my applications that happens a few times a month but has occurred twice this week. When this happens, it's always first thing in the morning when the first user loads the app and begins working (web application, 3-4 internal users) The error originates from this very simple method and once if fails, it will not work until I restart the application pool. Now, I'm querying AD in other ways as well but this is the first AD related method that's called when the users begin work in the morning.
public DomainUser GetDomainUser(string userLoginName)
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, this.DomainName))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, userLoginName))
{
// If user is null, the result is not a UserPrinciple
if (user != null)
{
string firstName = user.GivenName;
string middleName = user.MiddleName;
string lastName = user.Surname;
int empId = Convert.ToInt32(user.EmployeeId);
string emailAddr = user.EmailAddress;
string userName = user.SamAccountName;
DateTime? accountExp = user.AccountExpirationDate;
return new DomainUser
{
FirstName = firstName,
MiddleName = middleName,
LastName = lastName,
EmployeeId = empId,
Email = emailAddr,
UserName = userName,
AccountExpiration = accountExp
};
}
return null;
}
}
}
So this question is closely related but my permissions are setup correctly and the code works 99% of the time and will continue to run after an application pool restart.
Stack trace looks something like this:
System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue)
at ADWrapper.AdSearch.GetDomainUser(String userLoginName)
What could the problem be? Memory leaks? The common pattern is that this happens first thing in the morning when the first user begins using the app.