编程修改配置部分中等信任 [英] Modify configuration section programmatically in medium trust

查看:147
本文介绍了编程修改配置部分中等信任的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ConfigurationSection在我的应用程序:

I have a custom ConfigurationSection in my application:

public class SettingsSection : ConfigurationSection
{
    [ConfigurationProperty("Setting")]
    public MyElement Setting
    {
        get
        {
            return (MyElement)this["Setting"];
        }
        set { this["Setting"] = value; }
    }
}

public class MyElement : ConfigurationElement
{
    public override bool IsReadOnly()
    {
        return false;
    }

    [ConfigurationProperty("Server")]
    public string Server
    {
        get { return (string)this["Server"]; }
        set { this["Server"] = value; }
    }
}

在我的web.config

In my web.config

  <configSections>
    <sectionGroup name="mySettingsGroup">
      <section name="Setting" 
               type="MyWebApp.SettingsSection"  
               requirePermission="false" 
               restartOnExternalChanges="true"
               allowDefinition="Everywhere"  />
    </sectionGroup>
  </configSections>

  <mySettingsGroup>
    <Setting>
      <MyElement Server="serverName" />
    </Setting>
  </mySettingsGroup>

阅读部分工作正常。我遇到的问题是,当我通过阅读

Reading the section works fine. The issue I'm having is that when I read the section via

var settings = (SettingsSection)WebConfigurationManager.GetSection("mySettingsGroup/Setting");

然后我继续修改服务器属性:

   settings.Server = "something";

这不修改服务器属性在web.config文件中。

This doesn't modify the "Server" property in the web.config file.

请注意:这需要在中等信任的工作,所以我不能使用 WebConfigurationManager.OpenWebConfiguration 工作正常。有没有一个明确的方法来告诉 ConfigSection 来拯救自己?

Note: This needs to work under medium-trust, so I can't use WebConfigurationManager.OpenWebConfiguration which works fine. Is there an explicit way to tell the ConfigSection to save itself?

推荐答案

简短的回答 - 没有。 .NET队是(据称)意味着V4来解决这个问题,但它并没有发生。

Short answer - no. .NET team were (allegedly) meant to fix this in v4, but it didn't happen.

究其原因是因为使用 WebConfigurationManager.GetSection 收益嵌套只读的NameValueCollection s,这不会持续时你改变自己的价值观。使用 WebConfigurationManager.OpenWebConfiguration ,因为你已经相当正确地确定,是获得的唯一途径读写到配置的访问 - 但你会得到一个的FileIOPermission 抛出的异常,如 OpenWebConfiguration 尝试加载所有继承CONFIGS到你的web.config - 其中包括设备级网络。配置和machine.config中文件 C:\\ WINDOWS \\ Microsoft.NET \\框架,这是明确超出范围中等信任的

The reason is because using WebConfigurationManager.GetSection returns nested read-only NameValueCollections, which do not persist when you change their values. Using WebConfigurationManager.OpenWebConfiguration, as you've quite rightly ascertained, is the only way to obtain read-write access to the config - but then you'll get a FileIOPermission exception thrown, as OpenWebConfiguration attempts to load all inherited configs down to your web.config - which include the machine-level web.config and machine.config files in C:\WINDOWS\Microsoft.NET\Framework, which are explicitly out-of-scope of Medium Trust.

龙答案 - 用的XDocument / 的XmlDocument 和XPath获取/设置配置值

Long answer - use XDocument / XmlDocument and XPath to get/set config values.

这篇关于编程修改配置部分中等信任的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆