.NET 4.0:执行网络程序集

时间:2012-08-17 18:25:00

标签: .net .net-4.0 caspol

今天,我们将应用程序升级到.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);
        }
    }

1 个答案:

答案 0 :(得分:2)

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

相关问题