标签: system.configuration

如何创建包含集合集的配置节?

我需要一个像这样的配置部分:

<myConfig>
    <mySubConfig1>
        <mySubSubConfig1 keyAttribute="value1">
            <mySubSubConfig1Element keyAttribute="value1"/>
            <mySubSubConfig1Element keyAttribute="value2"/>
        </mySubSubConfig1>
        <mySubSubConfig1 keyAttribute="value2">
            <mySubSubConfig1Element keyAttribute="value1"/>
        </mySubSubConfig1>
    </mySubConfig1>
    <mySubConfig2>
        <mySubSubConfig2 keyAttribute="value1">
            <mySubSubConfig2Element keyAttribute="value1"/>
            <mySubSubConfig2Element keyAttribute="value2"/>
        </mySubSubConfig2>
        <mySubSubConfig2 keyAttribute="value2">
            <mySubSubConfig2Element keyAttribute="value1"/>
        </mySubSubConfig2>
    </mySubConfig2>
    <mySubConfig3>
        <mySubSubConfig3 keyAttribute="value1">
            <mySubSubConfig3Element keyAttribute="value1"/>
            <mySubSubConfig3Element keyAttribute="value2"/>
        </mySubSubConfig3>
        <mySubSubConfig3 keyAttribute="value2">
            <mySubSubConfig3Element keyAttribute="value1"/>
        </mySubSubConfig3>
    </mySubConfig3>
</myConfig>
Run Code Online (Sandbox Code Playgroud)

我还没有找到允许使用旧IConfigurationSectionHandler界面的魔法.有谁知道怎么做?

如果myConfigmySubConfignConfigurationSectionGroup或是,我可以ConfigurationSection.

此外,如果重要,将从web.config中使用它.

.net configuration system.configuration

35
推荐指数
1
解决办法
2万
查看次数

使用ASPNet_Regiis加密自定义配置部分 - 你能做到吗?

我有一个带有自定义配置部分的Web应用程序.该部分包含我想要加密的信息(希望使用ASPNet_RegIIS而不是自己做).

Web.Config中:

<?xml version="1.0"?>

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <configSections>
          <section name="MyCustomSection" 
                   type="MyNamespace.MyCustomSectionHandler, MyAssembly"/>
    </configSections>
<configProtectedData>
    <providers>
      <clear />
      <add name="DataProtectionConfigurationProvider"
           type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,
                   Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
                   processorArchitecture=MSIL"
           keyContainerName="MyKeyContainer"
           useMachineContainer="true" />
    </providers>
  </configProtectedData>
    <MyCustomSection>
       <blah name="blah1">
          <blahChild name="blah1Child1" />
       </blah>
    </MyCustomSection>
Run Code Online (Sandbox Code Playgroud)

配置处理程序在尝试加密之前工作得很好.当我尝试使用以下方法加密时:

aspnet_regiis -pef"MyCustomSection"c:\ inetpub\wwwroot\MyWebsite -prov DataProtectionConfigurationProvider

我收到一个错误:

加密配置部分...为MyCustomSection创建配置节处理程序时出错:无法加载文件或程序集"MyAssembly"或其依赖项之一.该系统找不到指定的文件.(c:\ inetpub\wwwroot\MyWebsite\web.config第5行)

我已尝试使用/不配置提供程序.有/无部分组.有/没有事先启动网站.我已经尝试暂时将我的程序集放入GAC进行注册.我也试过我的log4net部分只是尝试一些不是我的东西,没有运气.我以管理员身份运行命令提示符.有任何想法吗?或者ASPNet_RegIIS是否可以不用于自定义部分?

查看MSDN之后的最后一个镜头是将我的处理程序更改为继承自ConfigurationSection而不是实现IConfigurationSectionHandler,因为它在2.0中被技术上弃用(希望它是关于aspnet_regiis版本的东西).也没有运气.

任何想法让我知道.谢谢!

asp.net encryption web-config aspnet-regiis.exe system.configuration

31
推荐指数
2
解决办法
1万
查看次数

ConfigurationManager的命名空间在哪里?

我有一个引用System.Configuration- 并且ConfigurationSettings发现没有问题 - 但找不到类型或命名空间ConfigurationManager!?

我已经阅读过 - 看起来它应该属于与ConfigurationSettings类相同的命名空间.

编辑:请注意 - 我已经清楚地说我添加了对System.Configuration的引用.已有4条关于此的评论.

c# configurationmanager reference system.configuration .net-3.5

31
推荐指数
4
解决办法
7万
查看次数

ConfigurationManager.GetSection给出错误"无法从程序集中加载类型...."

我的app.config文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />
    </configSections>
    <ProcessConfiguration>
        <processes>
            <process name="Process1" />
        </processes>
    </ProcessConfiguration>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我有以下(单独)类来获取配置:

namespace Configuration
{
    using System.Configuration;

    public class ProcessesConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("processes", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(ProcessCollection))]
        public ProcessCollection Processes
        {
            get
            {
                return (ProcessCollection)base["processes"];
            }
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessCollection : ConfigurationElementCollection
    {
        public ProcessConfig this[int index]
        {
            get
            {
                return (ProcessConfig)BaseGet(index);
            }

            set
            {
                BaseAdd(index, value);
            }
        }

        protected …
Run Code Online (Sandbox Code Playgroud)

c# app-config system.configuration

24
推荐指数
1
解决办法
2万
查看次数

获取另一个Exe的App.Config

我有一个带App.Config文件的exe .现在我想在exe周围创建一个包装器dll,以便消耗一些功能.

问题是如何从包装器DLL访问exe中的app.config属性?

也许我应该在我的问题中多一点,我有以下app.config内容与exe:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="myKey" value="myValue"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

问题是如何从包装器dll中取出"myValue"?


谢谢你的解决方案.

实际上我最初的概念是避免使用XML文件读取方法或LINQ或其他任何方法.我首选的解决方案是使用配置管理器库等.

我将非常感谢使用通常与访问app.config属性相关联的类的任何帮助.

.net c# system.configuration configuration-files appsettings

22
推荐指数
2
解决办法
2万
查看次数

读取外部配置文件

我有一个执行FTP操作的ac..Net控制台应用程序.目前,我在自定义配置部分中指定设置,例如

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ftpConfiguration" type="FileTransferHelper.FtpLibrary.FtpConfigurationSection, FileTransferHelper.FtpLibrary" />
  </configSections>

  <ftpConfiguration>
      <Environment name="QA">
        <sourceServer hostname="QA_hostname"
                      username="QA_username"
                      password="QA_password"
                      port="21"
                      remoteDirectory ="QA_remoteDirectory" />
        <targetServer downloadDirectory ="QA_downloadDirectory" />

      </Environment>
  </ftpConfiguration>

</configuration>
Run Code Online (Sandbox Code Playgroud)

我想在命令行中指定一个外部配置文件.

然而!!!...

我刚刚意识到上面的"FtpConfiguration"部分并不真正属于应用程序的app.config.我的最终目标是,我将有许多执行我的控制台应用程序的计划任务,如下所示:

FileTransferHelper.exe -c FtpApplication1.config
FileTransferHelper.exe -c FtpApplication2.config
...
FileTransferHelper.exe -c FtpApplication99.config
Run Code Online (Sandbox Code Playgroud)

因此,我相信我走错了路,我真正想要的是在我的自定义xml文档中读取的内容,但继续使用System.Configuration获取值...而不是读取XmlDocument并将其序列化为获取节点/元素/属性.(虽然如果有人能给我看一些简单的代码,我不反对后者)

指针将不胜感激.谢谢.

更新:我接受的答案是另一个StackOverflow问题的链接,在这里用我的代码重复 - 下面正是我正在寻找的 - 使用OpenMappedExeConfiguration打开我的外部配置文件

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = @"D:\Development\FileTransferHelper\Configuration\SampleInterface.config";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

FtpConfigurationSection ftpConfig = (FtpConfigurationSection)config.GetSection("ftpConfiguration");
Run Code Online (Sandbox Code Playgroud)

c# configuration system.configuration

16
推荐指数
3
解决办法
6万
查看次数

为什么即使IsRequired为true,ConfigurationValidator也会验证ConfigurationProperty的默认值?

假设我有一个看起来像这样的配置属性.请注意,没有默认值.

[ConfigurationProperty("x", IsRequired = true)]
[StringValidator(MinLength = 1)]
public string X
{
    get { return (string)this["x"]; }
    set { this["x"] = value; }
}
Run Code Online (Sandbox Code Playgroud)

现在我添加我的部分如下:

<mySection x="123" />
Run Code Online (Sandbox Code Playgroud)

我会收到这个错误:

属性"x"的值无效.错误是:字符串长度必须至少为1个字符.

如果我更改配置属性以包含这样的默认值,它可以工作:

[ConfigurationProperty("x", DefaultValue="abc", IsRequired = true)]
[StringValidator(MinLength = 1)]
public string X
{
    get { return (string)this["x"]; }
    set { this["x"] = value; }
}
Run Code Online (Sandbox Code Playgroud)

这意味着即使IsRequired为true,验证器也会验证默认值.这也意味着我必须在我的所有属性上包含一个虚拟默认值来传递验证,即使它们实际上不会被使用.

这只是糟糕的设计还是有这种行为的正当理由?

.net c# system.configuration

12
推荐指数
1
解决办法
3758
查看次数

ConfigurationElementCollection具有许多不同类型的ConfigurationElements

是否可以使CollectionElementCollection具有许多不同类型的CollectionElements,例如:

<collection>
    <add type="MyType1, MyLib" Type1SpecificProp="1" />
    <add type="MyType2, MyLib" Type2SpecificProp="2" />
</collection
Run Code Online (Sandbox Code Playgroud)

我有这样的解决方案所需的所有类:

class MyCollection : ConfigurationElementCollection { }
class MyElement : ConfigurationElement { }
class MyType1 : MyElement { }
class MyType2 : MyElement { }
...
etc
Run Code Online (Sandbox Code Playgroud)

但是当我启动我的应用程序时,我会得到下一个可预测的错误:

无法识别的属性'Type1SpecificProp'.

因为Type1SpecificProp定义在MyType1没有MyElement,尤其是如果MyCollection有下一个方法:

protected override ConfigurationElement CreateNewElement()
{
    return new MyElement(); // but I want instantiate not the base class but by a type given
}
Run Code Online (Sandbox Code Playgroud)

即返回基类因此OnDeserializeUnrecognizedAttribute()在子类中从未被调用过.

所以问题是:如何让孩子们的班级通过自己解决未知元素?

.net c# app-config system.configuration custom-configuration

10
推荐指数
2
解决办法
3857
查看次数

简单的自定义配置部分,包含.NET4中的集合

我正在尝试为.NET4应用程序编写一个非常简单的自定义配置部分.我的目标是:

<configuration>
  <configSections>
    <section name="myServices" type="My.ConfigSection, My.Assembly" />
  </configSections>
  <myServices>
    <add name="First" />
    <add name="Second" />
  </myServices>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是,ConfigurationErrorsException当我打电话时,我不断得到:'无法识别的元素'添加'' ConfigurationManager.GetSection("myServices").我已经盯着它看了一段时间,但还没弄明白我做错了什么.以下是我的代码.这是三大类:ConfigSection,MyServiceSettingsCollectionMyServiceSettings.

首先是代表整个配置部分的类.它有一个无名的默认类型集合MyServiceSettingsCollection.该IsDefaultCollection属性应该允许我从根元素直接"添加"到我的集合.

public sealed class ConfigSection : ConfigurationSection
{
  private static readonly ConfigurationProperty _propMyServices;

  private static readonly ConfigurationPropertyCollection _properties;

  public static ConfigSection Instance { get { return _instance; } }

  static ConfigSection()
  {
    _propMyServices = new ConfigurationProperty(
          null, typeof(MyServiceSettingsCollection), null,
          ConfigurationPropertyOptions.IsDefaultCollection);
    _properties = new ConfigurationPropertyCollection { _propMyServices }; …
Run Code Online (Sandbox Code Playgroud)

.net c# system.configuration .net-4.0

10
推荐指数
2
解决办法
1万
查看次数

TypeLoadException

我正在使用app.config文件来存储凭据,当我尝试检索它们时,我得到TypeLoadException如下:

无法从程序集'System.Configuration,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'加载类型'System.Configuration.DictionarySectionHandler'

这是一个.NET 4.5项目,我设置SystemSystem.Configuration Copy-Local属性true,我不明白问题的来源.我对.NET编程没有经验,所以对汇编的概念不太满意.

以下是代码片段:

的app.config

<configSections>
  <sectionGroup name="Credentials">
   <section name="Twitter" type="System.Configuration.DictionarySectionHandler"/>
  </sectionGroup>
</configSections>

<Credentials>
 <Twitter>
   <add key="****" value="*****"/>
   <add key="****" value="*****"/>
  </Twitter>
</Credentials>
Run Code Online (Sandbox Code Playgroud)

连接服务文件

var hashtable = (Hashtable)ConfigurationManager.GetSection("Credentials/Twitter");
Run Code Online (Sandbox Code Playgroud)

我知道这是一个常见的问题,我在发布之前用Google搜索了它.但到目前为止我发现的所有解决方案似乎都不起作用,或者我可能没有正确理解它们.

先感谢您.

c# exception system.configuration .net-assembly

10
推荐指数
1
解决办法
2436
查看次数