【问题标题】:.NET 4.0: Executing network assemblies.NET 4.0:执行网络程序集
【发布时间】:2012-08-17 18:24:59
【问题描述】:

今天,我们将应用程序升级到 .NET Framework 4.0。 当它调用驻留在网络共享上的程序集时,对于以前的版本,我们需要以下命令:

 caspol.exe -m -chggroup 1.3 -zone "Intranet" FullTrust

对于 .NET 4,我们阅读了“NetFx40_LegacySecurityPolicy”并将其包含在 App.config 文件之外。

<runtime>
    <NetFx40_LegacySecurityPolicy enabled="true"/>
    <loadFromRemoteSources enabled="true"/>
</runtime>

不幸的是,这不起作用:一旦我们的应用程序启动,我们就会收到一个异常,指出我们无法访问环境变量(缺少 System.Security.Permissions.EnvironmentPermission)。

我们使用 CasPol.exe,但无法弄清楚我们必须做什么才能让我们的应用程序访问环境变量。删除 Environment.GetEnvironmentVariable 调用仍然不能解决问题 - 似乎很多其他操作也不起作用。

移除 NetFx40_LegacySecurityPolicy 开关(或将其设置为 false)允许我们再次读取环境,但(当然)会阻止在网络共享上执行程序集。

这是我们完整的 App.config 文件:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="Launcher.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Launcher.Properties.Settings>
            <setting name="Executable" serializeAs="String">
                <value>\\office\client\client-8919\Client.exe</value>
            </setting>
        </Launcher.Properties.Settings>
    </applicationSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <runtime>
        <NetFx40_LegacySecurityPolicy enabled="true"/>
        <loadFromRemoteSources enabled="true"/>
    </runtime>
</configuration>

编辑:

这是我们用于启动驻留在网络共享上的程序集的代码:

    public void ExecuteFile(string version, string[] args)
    {
        try
        {
            String appPath = GetExecutablePath();
            if (!Directory.Exists(appPath))
                throw new Exception("cache does not contain expected executable directory: " + appPath);

            String executable = appPath + "\\Client.exe";
            if (!File.Exists(executable))
                throw new Exception("cache does not contain expected executable: " + executable);

            if (Program.DEBUG_MODE)
                MessageBox.Show("App Path: " + appPath + "\r\nExecutable: " + executable);

            AppDomainSetup domainInfo = new AppDomainSetup();
            domainInfo.ApplicationBase = appPath;
            AppDomain subDomain = AppDomain.CreateDomain("Name", AppDomain.CurrentDomain.Evidence, domainInfo);

            subDomain.ExecuteAssembly(executable, subDomain.Evidence, args);
        }
        catch (Exception e)
        {
            MessageBox.Show("Fehler beim Ausführen der Version im lokalen Cache!\r\n" + e.Message);
        }
    }

【问题讨论】:

  • 您不需要进行任何调整 - .NET 4 的更改之一是 all local intranet applications are granted full trust by default
  • 我们成功地将null 作为CreateDomain 调用的证据并使用不需要evidence 参数的ExecuteAssembly 重载。给定您通过的证据值,结果应该是相同的。
  • 克里斯蒂安,非常感谢!你的小费成功了。也许您可以将您的文字复制为答案,以便我接受?
  • 达米恩,非常感谢您的评论。您是对的,使用 Christian 的修改(传入 null 作为证据)我们不需要像这些配置选项那样进行任何调整。

标签: .net .net-4.0 caspol


【解决方案1】:

抱歉,这不是一个“好”的答案。但是我们成功地将null 作为CreateDomain 调用的证据并使用不需要evidence 参数的ExecuteAssembly 重载。给定您传递的证据值,结果应该是相同的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多