System.DirectoryServices 中的间歇性异常

发布于 2025-01-08 12:57:16 字数 1796 浏览 0 评论 0原文

我有一个奇怪的 System.DirectoryServices 问题,它间歇性地弹出。

定期抛出以下异常

    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 subsupSecurityChecks) 中 System.Reflection.RuntimeAssembly.LoadWithPartialNameInternal(AssemblyName an、Evidence securityEvidence、StackCrawlMark& stackMark)位于 System.DirectoryServices.AccountManagement.UnsafeNativeMethods.IADsPathname.Retrieve(Int32 lnFormatType)位于 System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName() 在 System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p) 在 System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper()

也很奇怪的是 Exception.Message 它是: 无法加载文件或程序集“MyCustomAssembly.XmlSerializers”或其依赖项之一。系统找不到指定的文件

有趣的是,该程序集中甚至没有引用 MyCustomAssembly。

我认为 Exception.Message 与调试信息不​​匹配,实际的 Stacktrace 或多或少是正确的异常。

知道为什么会发生这种情况吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2025-01-15 12:57:17

我知道你很久以前就问过这个问题,但我自己也遇到过这个问题,并通过我的构建配置管理器并确保所有项目都针对相同的 CPU 来解决它 - 我有 2 个构建为“任何 CPU”和一个 Windows Forms 测试针对 x86 构建的应用程序。我将其更改为“Any CPU”,问题就消失了。

此处

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

云淡风轻 2025-01-15 12:57:17

最近,当我们将服务器升级到 .NET 4.5 框架时,我们就遇到了这种情况(我们遇到了 FileNotFoundExceptionNotSupportedException)。我们的解决方案以 .NET 4 为目标,因此我相信当 .NET 4.5 尝试运行 .NET 4 应用程序时可能存在一些向后兼容性问题,并且连接到Active Directory

将解决方案切换到目标 4.5 并重新发布到服务器似乎可以解决问题。

以下是如何实现这一目标。

确保解决方案中的所有项目都以 4.5 为目标:

右键单击您的项目 -> 属性 -> 应用程序选项卡

在此处输入图像描述

同时确保 IIS 正在使用正确的框架版本(发布时,它应该以 4.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.):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文