今天,我在嘗試遠程調試爲.NET 4.0運行時構建的應用程序時遇到了一個奇怪的問題。網絡共享上的.NET 4.0應用程序導致SecurityException
該應用程序駐留在網絡共享上並由遠程機器執行。但是,由於System.Configuration.ConfigurationManager.GetSection()方法中的權限需求引發SecurityException,應用程序在加載過程中每次都會崩潰。我還沒有檢查基類庫中的其他許可要求是否也會導致安全異常,但在所有情況下,這都不會發生在新CLR中。
應用程序以完全信任的方式運行(在調試過程中對其進行檢查,並且像往常一樣,CLR 4.0中的Intranet應用程序必須始終爲真),所以我無法理解權限需求在這種情況下如何導致異常。當針對3.5 SP1運行時(默認情況下首先完全信任網絡共享應用程序)構建時,所有內容都按預期運行。
我粘貼下面的示例代碼。任何幫助是極大的讚賞。
using System;
using System.Configuration;
namespace ConsoleApplication1
{
public sealed class AssetsSection : ConfigurationSection
{
private static readonly ConfigurationProperty s_propPath;
private static readonly ConfigurationPropertyCollection s_properties;
static AssetsSection()
{
s_propPath = new ConfigurationProperty("path", typeof(String));
s_properties = new ConfigurationPropertyCollection()
{
s_propPath
};
}
public static AssetsSection Get()
{
return (AssetsSection) ConfigurationManager.GetSection("test/assets");
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return s_properties;
}
}
public String Path
{
get
{
return (String) base[s_propPath];
}
set
{
base[s_propPath] = value;
}
}
}
class Program
{
static void Main(String[] args)
{
Console.WriteLine(AssetsSection.Get().Path);
Console.ReadLine();
}
}
}
和App.config文件;
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="test">
<section name="assets" type="ConsoleApplication1.AssetsSection, ConsoleApplication1"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>
<test>
<assets path="..\Assets"/>
</test>
</configuration>
你爲什麼要建立.NET 4.0,但迫使它運行舊版本的CLR的? – 2010-04-28 00:26:02
對不起,我從我的測試代碼粘貼了錯誤的配置文件。我編輯了這個問題。然而,問題仍然存在。 – David 2010-04-28 07:44:10