我有一些奇怪的事情,我希望 .Net 专家可以帮助我。
我有一个自定义配置部分,为了掌握它,我这样做:
var s = (TestConfigurationSection)ConfigurationManager
.GetSection("testSection");
我在我的开发机器上运行它(Windows 7
64位,Windows完全是最新的),它工作正常。
我将带有该代码的exe放入其中,然后将其放在c:\users\public
机器上的目录中Windows Server 2008 R2
,以管理员身份打开命令提示符,运行它,我得到:
System.Configuration.ConfigurationErrorsException:为 testSection 创建配置节处理程序时出错:请求失败。(C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.Config 第 10 行)---> System.Security.SecurityException:请求失败。
现在我更改了该代码来执行此操作:
var config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
var s = (TestConfigurationSection) config
.GetSection("testSection");
它在两台机器上都能正常工作。
所以,我很高兴(尽管我的应用程序正在运行)但我脑海中的那个小 Gremlin 很困惑,所以我在这里问:
为什么会这样?
重现步骤
在 Visual Studio 2010 中创建一个名为 AppConfigTestConsoleApplication 的新 .net 4 控制台应用程序项目,并将其内容替换为Program.cs
以下内容:
using System;
using System.Configuration;
namespace AppConfigTestConsoleApplication
{
public class TestConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("someSetting")]
public int SomeSetting
{
get { return (int) this["someSetting"]; }
set { this["someSetting"] = value; }
}
}
internal class Program
{
private static void Main()
{
try
{
var s = (TestConfigurationSection) ConfigurationManager
.GetSection("testSection");
Console.WriteLine("First Method worked: " + s.SomeSetting);
}
catch (Exception ex)
{
Console.WriteLine("First method failed");
Console.WriteLine(ex.ToString());
if (ex.InnerException != null)
{
var eex = ex.InnerException as SecurityException;
Console.WriteLine("Action: '{0}'", eex.Action.ToString());
Console.WriteLine("Demanded: '{0}'", eex.Demanded.ToString());
Console.WriteLine("RefusedSet: '{0}'", eex.RefusedSet);
Console.WriteLine("GrantedSet: '{0}'", eex.GrantedSet);
}
try
{
var config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
var s = (TestConfigurationSection) config
.GetSection("testSection");
Console.WriteLine("Second Method worked: "
+ s.SomeSetting);
}
catch (Exception x)
{
Console.WriteLine("Even the second method failed!");
Console.WriteLine(ex.ToString());
}
}
}
}
}
然后添加一个应用程序配置文件并将内容替换为以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="testSection"
type="AppConfigTestConsoleApplication.TestConfigurationSection, AppConfigTestConsoleApplication"
requirePermission="false"
allowDefinition="Everywhere" />
</configSections>
<testSection someSetting="10"></testSection>
</configuration>
编译并运行,这是我得到的输出:
C:\Users\Public\configtest>AppConfigTestConsoleApplication.exe
First method failed
System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for testSection: Request failed. (C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.Config line 10) ---> System.Security.SecurityException: Request failed.
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(ConstructorInfo ctor)
at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)
--- End of inner exception stack trace ---
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecordsectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at AppConfigTestConsoleApplication.Program.Main()
Action: 'Demand'
Demanded: '<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
'
RefusedSet: ''
GrantedSet: ''
Second Method worked: 10
进程监视器
我运行Process Monitor并像这样设置过滤器:
剩下的 508 个事件都是以下事件之一:
- 未找到姓名
- 没有更多的条目
- 未找到路径
- 只有读者锁定的文件
- 没有这样的文件(对于C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\93e7df09dacd5fef442cc22d28efec83\mscorlib.ni.dll和C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.config只有一次)
- 缓冲区溢出(对于HKCU\Control Panel\Desktop\MuiCached\MachinePreferredUILanguages,HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Cache,
有没有人对设置什么过滤器来找到根本原因有任何建议?