c# - 为什么我的 C# AppDomain 前一秒还好,下一秒就抛出异常?

标签 c# remoting appdomain

我有一个 AppDomain,我用它来将模块加载到沙箱中:

class PluginLoader
{
    public static AppDomain PluginSandbox;

    static PluginLoader()
    {
        AppDomainSetup ads = new AppDomainSetup();
        ads.ApplicationName = "Plugin Modules";
        PermissionSet trustedLoadFromRemoteSourceGrantSet = 
                     new PermissionSet(PermissionState.Unrestricted);
        PluginSandbox = 
                     AppDomain.CreateDomain("Plugin App Domain", 
                     null, ads, trustedLoadFromRemoteSourceGrantSet);
     }

稍后,我将拉入我需要的 DLL 并创建一个对象实例:

     public IPlugin FindPlugin(string pluginName)
     {
          ObjectHandle handle = 
                   PluginSandbox.CreateInstance(pluginName, 
                       "Plugins." + pluginName);
                IPlugin ip = (IPlugin)handle.Unwrap();
                return ip;
     }

我试过几次都没有问题。在沙盒中获取各种对象的实例,没有任何问题。

稍后在代码中,在另一种方法中,我需要找到程序集以获取嵌入式资源(在数据文件中编译,带有 ManifestResource)。所以我打电话:

     Assembly [] ar = PluginSandbox.GetAssemblies();

错误被抛出:

A first chance exception of type 'System.IO.FileNotFoundException' 
occurred in PluginRunner.dll.

Additional information: Could not load file or assembly '10wl4qso,
Version=1.0.3826.25439, culture info=neutral, PublicKeyToken=null'
or one of its dependencies.  The system cannot find the file specified.

我并不感到惊讶。 “10wl4qso”不是程序集、dll 或类似名称的名称。事实上,每次运行似乎都是伪随机的。加上 GetAssemblies 的附加乐趣甚至没有记录以抛出此异常。

现在我可以在获得初始对象后立即调用 GetAssemblies,一切都很好。但是几秒钟后,我用另一种方法得到了这个。由于处于远程状态,PluginSandbox 在调试器中没有任何有用的信息。

我在 AppDomain 上捕捉到 UnhandledException 和 DomainUnload,但都没有被触发。

为什么我的 AppDomain 突然不知道它的程序集? 哪里来的垃圾数据? 我能做些什么来防止这两种情况的发生?

最佳答案

您看到的这个奇怪的命名程序集可能是由 XmlSerializer 生成的。 XML 序列化器将输出一个动态程序集,以便能够快速序列化和反序列化特定类型。检查您的代码是否使用了 XmlSerializer,将它们注释掉并查看问题是否再次出现。

关于c# - 为什么我的 C# AppDomain 前一秒还好,下一秒就抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3105498/

相关文章:

multithreading - ASP.NET 网站中出现意外的第二个 AppDomain

c# - 尝试访问已卸载的应用程序域(调用 AzMan)

c# - 如何制作 IsNull() 方法

c# - Linq,如何使用子查询?

delphi - 使用 Delphi 实现透明远程处理的最简单解决方案是什么?

.net - 为什么 IpcChannel 告诉我, "Cannot open an anonymous level security token?"

c# - 在默认 AppDomain 中托管在 Winforms 中的 WPF 用户控件在弹出窗口中损坏的选项卡导航

c# - 对于WPF,免费烛台示例?

c# - 如何防止嵌入资源的资源程序集生成

.net - 远程服务器自动发现。广播与否?