4

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?

Fox
  • 891
  • 3
  • 9
  • 30
  • This is normal. Debug + Exceptions, untick the Thrown box for CLR exceptions. – Hans Passant Feb 23 '12 at 12:10
  • possible duplicate of [FileNotFoundException in ApplicationSettingsBase](http://stackoverflow.com/questions/3494886/filenotfoundexception-in-applicationsettingsbase) – Hans Passant Feb 23 '12 at 12:12
  • Are you saying I should just catch and ignore that exception? – Fox Feb 24 '12 at 07:28
  • Exception above is not happening in Debug. I am not debugging the app. This exception is raised in runtime. Web app is published to IIS. – Fox Feb 24 '12 at 08:54
  • This issue causes tests to fail in our automated test system! It doesn't happen when I run the tests on my developer machine. The code which triggers the exception is exactly in an ActiveDirectory principal handling code. It's not a solution for us to untick any Exception in the Debug menu, since it doesn't happen in my debugger. – Csaba Toth Jun 28 '13 at 16:02
  • It's great that the System supposedly catches the exception, but why it fails our test then? It's an NUnit test. – Csaba Toth Jun 28 '13 at 16:04
  • @HansPassant The SO article you mention talks about a case when the system searches for a *.XmlSerializers within the .NET system itself. In our case the system searches an unexistent MyCustomAssembly.XmlSerializers – Csaba Toth Jun 28 '13 at 16:06
  • It just works the exact same way for types in your own assembly, it isn't exclusive to .NET types. – Hans Passant Jun 28 '13 at 18:59

2 Answers2

4

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

AussieSven
  • 41
  • 2
0

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.):

Darren
  • 68,902
  • 24
  • 138
  • 144