c# - 为什么新的 AppDomain 会加载不必要的 DLL?

标签 c# asp.net .net-assembly appdomain

我有一个 asp.net web api,它具有执行一些动态生成的 DLL 的逻辑。 我想创建一个具有受限权限集的新沙盒 AppDomain 来执行这些 DLL。

这是创建新的受限应用域的方法:

public static AppDomain CreateRestrictedDomain(string applicationBasePath)
{
    PermissionSet permissionSet = new PermissionSet(PermissionState.None);
    permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
    permissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, applicationBasePath));

    AppDomainSetup appDomainSetup = new AppDomainSetup
    {
        ApplicationBase = applicationBasePath,
        DisallowCodeDownload = true,
        DisallowBindingRedirects = true,
        DisallowPublisherPolicy = true,
        LoaderOptimization = LoaderOptimization.MultiDomainHost,
    };


    AppDomain restrictedDomain = AppDomain.CreateDomain(
        friendlyName: "MySandbox",
        securityInfo: null,
        info: appDomainSetup,
        grantSet: permissionSet,
        fullTrustAssemblies: null);

    return restrictedDomain;
}

我遇到的问题是第一次引用新的 appDomain 时,例如只是为了读取 appDomain.FriendlyName,框架会尝试将当前应用程序中存在的一些依赖项加载到新的 AppDomain。如果依赖项不在 GAC 中,它会尝试在 applicationBasePath 中查找它们,但会失败,因为 applicationBasePath 设置为仅存储不受信任的 DLL 的文件夹。

我的问题是为什么创建新的 AppDomain 会触发加载这些额外的依赖项。在我上面的 CreateRestrictedDomain 方法中,没有引用任何这些依赖项。

我在 VS 2013 中使用默认模板创建了一个新的 asp.net 项目,在 Index Controller 中我只是使用上面的代码创建了一个新的 AppDomain,并打印出名称。这是代码:

public ActionResult Index()
{
    var tempPath = Path.GetTempPath();
    var untrustedPath = Path.Combine(tempPath, "unstrusted");
    if (!Directory.Exists(untrustedPath))
    {
        Directory.CreateDirectory(untrustedPath);
    }
    var appDomain = AppDomainSandboxHelper.CreateRestrictedDomain(untrustedPath);
    var friendlyName = appDomain.FriendlyName;
    ViewBag.Title = friendlyName;

    return View();
}

当我在 Debug模式下运行时,我可以看到读取 appDomain.FriendLyName 的行会触发 System.Web.dll 加载到新的 AppDomain“MySandbox”中

'iisexpress.exe' (CLR v4.0.30319: Domain 4): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Symbols loaded.

'iisexpress.exe' (CLR v4.0.30319: MySandbox): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_64\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll'. Symbols loaded.

为什么System.Web.dll需要加载到新的AppDomain中?

最佳答案

securityInfo 传递 null 使用当前正在执行的 AppDomain 的证据。

AppDomain.CreateDomain :

securityInfo

Type: System.Security.Policy.Evidence

Evidence that establishes the identity of the code that runs in the application domain. Pass null to use the evidence of the current application domain.

很可能需要加载 System.Web.dll 程序集以检查其证据。

关于c# - 为什么新的 AppDomain 会加载不必要的 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29706042/

相关文章:

c# - 查看我的应用程序正在运行多少个线程?

c# - 使用线程池或线程

Javascript 无法识别 Asp Checkbox Checked 属性

c# - 如何修复从不同网络登录网站帐户时出现的错误

Jquery 自动完成在 Enter 按键时触发

c# - 如何找到引用错误版本程序集的内容并修复它?

c# - 有没有一种方法可以在不提供任何 C# 信息的情况下在网络上找到 Active Directory?

c# - 如何使用 TestDriven.NET 指定测试方法参数?

c# - 无需解包即可从存档中获取程序集文件版本

C# 输出程序集文件名 (csproj)