Fox
Fox

Reputation: 891

Intermittant Exception in System.DirectoryServices

I have a strange System.DirectoryServices Issue which pops up intermittantly.

The exception below gets thrown periodically in the below code

    private PrincipalSearchResult<Principal> GetAuthorizationGroups(UserPrincipal userPrincipal, int tries)
    {
        try
        {
            //Exception is thrown on this line below
            return userPrincipal.GetAuthorizationGroups();
        }
        catch (AppDomainUnloadedException ex)
        {
            if (tries > 5)
            {
               throw;
            }
            tries += 1;
            Thread.Sleep(5000);
            return GetAuthorizationGroups(userPrincipal, tries);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

Exception Stacktrace at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.LoadWithPartialNameInternal(AssemblyName an, Evidence securityEvidence, StackCrawlMark& stackMark) at System.DirectoryServices.AccountManagement.UnsafeNativeMethods.IADsPathname.Retrieve(Int32 lnFormatType) at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName() at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p) at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper()

Something also very strange is the Exception.Message which is : Could not load file or assembly 'MyCustomAssembly.XmlSerializers' or one of its dependencies. The system cannot find the file specified

The funny thing is that MyCustomAssembly is not even referenced in this assembly.

I think the Exception.Message is mismatching the Debug info and the actual Stacktrace is more or less the correct Exception.

Any idea why this is happening?

Upvotes: 4

Views: 1820

Answers (2)

Darren
Darren

Reputation: 70776

This has recently happened to us when we upgraded the server to the .NET 4.5 framework (We were encountering FileNotFoundException and NotSupportedException). Our solution was targeting .NET 4 so I believe there maybe some backwards compatibility issue when .NET 4.5 tries to run a .NET 4 application and connect to Active Directory.

Switching the solution to target 4.5 and republishing to the server seemed to do the trick.

Below is how to achieve this.

Ensure that all projects in your solution target 4.5:

Right click on your project -> Properties -> Application Tab

enter image description here

Also ensure that IIS is using the correct framework version (At time of posting, it should target 4.0.):

Upvotes: 0

AussieSven
AussieSven

Reputation: 41

I know you asked this a long time ago but I just had this problem myself and solved it by going through my build configuration manager and making sure all the projects were targeting the same CPU - I had 2 building to "Any CPU" and a Windows Forms test app building towards x86. I changed this to Any CPU and the problem vanished.

Got the tip from here

Upvotes: 4

Related Questions