今天我在尝试远程调试一款针对.NET 4.0运行时构建的应用程序时遇到了一个奇怪的问题。
应用程序位于网络共享上,并由远程计算机执行。但是,每次在加载期间由System.Configuration.ConfigurationManager.GetSection()方法引发的权限要求导致SecurityException而导致应用程序崩溃。我还没有检查基类库中是否存在其他权限要求也会导致安全异常,但在所有情况下,这不应该在新CLR中发生。
该应用程序在完全信任下运行(在调试过程中检查并且通常对于CLR 4.0中的内部网络应用程序来说,这必须始终为真),因此我不知道在这种情况下如何通过权限要求引起异常。当针对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>