标签: configurationelement

自定义配置,ConfigurationElements和ConfigurationProperties

过去3天我一直在网上搜索,但找不到任何关于这个问题的提法.我已经创建了一个自定义配置类,可以与我的app.config一起使用.一切正常.当不需要配置属性(配置元素)并且未在app.config中定义时,会出现此问题.似乎为配置属性返回默认值.有谁知道如何确定app.config中是否未定义该属性?(我一直试图发布我的app.config,但无法弄清楚如何做到这一点......谁知道怎么做?)


//Main
namespace TestStub
{
    class Program
    {
        static void Main(string[] args)
        {
            CustomSettingsHandler config = (CustomSettingsHandler)ConfigurationManager.GetSection("CustomSettingsManager");
            Console.WriteLine("Setting1 {0}", config.Setting1.CustomSettingItem);
            Console.WriteLine("Setting2 {0}", config.Setting2.CustomSettingItem);
        }
    }
}

//Custom Configuration Class
namespace CustomConfiguration
{
    public class CustomSettingsHandler : ConfigurationSection
    {
        [ConfigurationProperty("setting1", IsRequired = false)]
        public CustomSettingElement Setting1 { get { return (CustomSettingElement)this["setting1"]; } }

        [ConfigurationProperty("setting2", IsRequired = false)]
        public CustomSettingElement Setting2 { get { return (CustomSettingElement)this["setting2"]; } }
    }

    public class CustomSettingElement : ConfigurationElement
    {
        [ConfigurationProperty("customsettingitem", IsRequired = false)]
        public int CustomSettingItem …
Run Code Online (Sandbox Code Playgroud)

c# app-config configurationsection configurationelement

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

使用带有.Net配置框架的必需子ConfigurationElement加载ConfigurationSection

我有一个控制台应用程序试图从web.config文件加载CustomConfigurationSection.

自定义配置部分具有所需的自定义配置元素.这意味着当我加载配置部分时,如果配置中不存在该配置元素,我希望看到异常.问题是.NET框架似乎完全忽略了isRequired属性.因此,当我加载配置部分时,我只创建自定义配置元素的实例并将其设置在配置部分.

我的问题是,为什么会发生这种情况?我希望GetSection()方法触发ConfigurationErrors异常,因为配置中缺少必需的元素.

以下是我的配置部分的外观.

public class MyConfigSection : ConfigurationSection
{
    [ConfigurationProperty("MyConfigElement", IsRequired = true)]
    public MyConfigElement MyElement
    {
        get { return (MyConfigElement) this["MyConfigElement"]; }
    }
}
public class MyConfigElement : ConfigurationElement
{
    [ConfigurationProperty("MyAttribute", IsRequired = true)]
    public string MyAttribute
    {
        get { return this["MyAttribute"].ToString(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我加载配置部分的方法.

   class Program
    {
        public static Configuration OpenConfigFile(string configPath)
        {
            var configFile = new FileInfo(configPath);
            var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
            var wcfm = new WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add("/", vdm);
            return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
        } …
Run Code Online (Sandbox Code Playgroud)

c# configuration configurationsection configurationelement webconfigurationmanager

11
推荐指数
1
解决办法
4254
查看次数

如何使用IIS 7 API将HttpError ConfigurationElement恢复为"Inherited"

假设我有以下<httpErrors>集合web.config:

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

是的,很好'空.

在IIS 7中,我的HTTP错误页面如下所示:

httpErrors

美丽!(我突然强调了404,因为这是我将在一秒内使用的例子).

现在,我运行以下代码:

errorElement["statusCode"] = 404;
errorElement["subStatusCode"] = -1;
errorElement["path"] = "/404.html";
httpErrorsCollection.Add(errorElement);
Run Code Online (Sandbox Code Playgroud)

可爱.我现在按照预期在我的web.config:

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/404.html" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)

不能更快乐.现在,在IIS 7中,我的HTTP错误部分看起来像预期的那样,如下所示: 替代文字

在这一点上,生活不能更甜蜜.现在,在线下,我想以编程方式将我的404错误恢复到原始屏幕截图中显示的状态.逻辑规定我应该remove犯我的新错误:

httpErrorsCollection.Remove(errorElement);
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做,我web.config看起来很像这样:

    <httpErrors>
        <remove statusCode="404" subStatusCode="-1" />
    </httpErrors>
Run Code Online (Sandbox Code Playgroud)

我的IIS看起来有点像这样:

替代文字

这是因为我的web.config- 但是如何,使用ServerManager和所有它有用的IIS 7 API,我是否httpError完全删除元素并将其恢复web.config为:

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

有任何想法吗?

.net iis-7 configurationelement servermanager

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

如何在后代元素上使用.NET自定义ConfigurationElement属性?

如何在后代CustomSetting元素中获取和使用父ConfigurationSection中的属性集?当CustomSetting元素返回Value属性时,我需要此属性.

我想像这样格式化App.config:

<CustomSettings someProperty="foo">
    <CustomSetting key="bar" value="fermeneba" />
    <CustomSetting key="laa" value="jubaduba" />
</CustomSettings>
Run Code Online (Sandbox Code Playgroud)

我有代码工作,除了我找不到从CustomSetting类访问someProperty属性的方法.到目前为止,我找到的唯一方法是格式化这样的配置,这很麻烦:

<CustomSettings>
    <CustomSetting someProperty="foo" key="bar" value="fermeneba" />
    <CustomSetting someProperty="foo" key="laa" value="jubaduba" />
</CustomSettings>
Run Code Online (Sandbox Code Playgroud)

.net configuration app-config configurationsection configurationelement

7
推荐指数
1
解决办法
7136
查看次数

从ConfigurationElementCollection获取配置元素

我(希望)设置我自己的设计的ConfigurationElementCollection,电子邮件作为键.怎么办?很难在网上找到.我如何能:

  1. 通过它迭代?

  2. 查看是否存在特定元素?

  3. 得到一个特定的元素?

...给出:

    YourConfigElement config = 
   ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
Run Code Online (Sandbox Code Playgroud)

部分答案

1.

 foreach (X x in config.XCollection) 
             <code here>
Run Code Online (Sandbox Code Playgroud)

2.用"替换此处的代码"代替

 { 
    if (x.Y == needle) 
    {
       hasIndeed = true;
       break;
    }
 }
Run Code Online (Sandbox Code Playgroud)

3.用"替换此处的代码"代替

 { if (x.Y == needle) 
       cameUpWith = x;
       break;
 }
Run Code Online (Sandbox Code Playgroud)

微小的气味.

c# system.configuration configurationelement

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

WCF无法序列化.NET基类型

我正在编写WCF服务,并希望公开一些自定义配置元素(例如Custom ConfigurationSection和ConnectionStringSettings),以便我可以修改服务的配置.

我的一个自定义配置元素继承自System.Configuration.ConfigurationElementCollection.当我尝试启动我的WCF服务时,我收到以下错误消息...

类型'System.Configuration.ConfigurationElementCollection'无法序列化.请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员.

有没有办法为此类型实现DataContract?我有我的继承类标有[DataContract]属性.

wcf serialization datacontract datamember configurationelement

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

ConfigurationElement 中的 DateTime 属性

我想将 DateTime 放入配置文件,但是,我希望 DateTime 以特定方式表示。我已经看到在 ConfigurationElement 中使用 DateTime 的示例(如下例所示)。我见过的例子都有以美国格式表示的日期。我想确保所有人都能理解日期,无论他们是谁,所以我想将其yyyy-MM-dd HH:mm:ss用作格式。

使用从 ConfigurationElement 派生的类时如何做到这一点?

    class MyConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("Time", IsRequired=true)]
        public DateTime Time
        {
            get
            {
                return (DateTime)this["Time"];
            }
            set
            {
                this["Time"] = value;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# datetime web-config configurationelement

5
推荐指数
1
解决办法
3664
查看次数

ConfigurationSection多个枚举值

有没有办法在配置部分设置多个枚举值?

就像在.net中一样 object.Filter = Filter.Update | Filter.Create;

<wacther filter="update, created"/>
Run Code Online (Sandbox Code Playgroud)

是否支持这样的东西?

.net c# configuration configurationsection configurationelement

3
推荐指数
1
解决办法
6462
查看次数

无法为自定义 ConfigurationElement 类型的 ConfigurationProperty 设置 DefaultValue

我对 ConfigurationProperty 的 DefaultValue 有一个小问题。

这是我的 XML 配置的一部分:

<Storage id="storageId">
    <Type>UNC</Type>
</Storage>
Run Code Online (Sandbox Code Playgroud)

为了处理此配置,我创建了“StorageElement:ConfigurationElement”:

public class StorageElement : ConfigurationElement
{
    private static readonly ConfigurationPropertyCollection PropertyCollection = new ConfigurationPropertyCollection();

    internal const string IdPropertyName = "id";
    internal const string TypePropertyName = "Type";

    public StorageElement()
    {
        PropertyCollection.Add(
            new ConfigurationProperty(
                IdPropertyName, 
                typeof(string), 
                "", 
                ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey
                ));

        PropertyCollection.Add(
            new ConfigurationProperty(
                TypePropertyName,
                typeof(ConfigurationTextElement<string>),
                null,
                ConfigurationPropertyOptions.IsRequired));           
    }

    public string Id 
    { 
        get
        {
            return base[IdPropertyName] as string;
        }
    }

    public string Type
    {
        get
        {

            return (base[TypePropertyName] …
Run Code Online (Sandbox Code Playgroud)

.net c# configuration configurationelement

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