我已经编写了一些自定义配置集合,元素等等.现在,我想做一个简单的Linq语句:
ServerDetails servers = ConfigurationManager.GetSection("serverDetails") as ServerDetails;
var server = from s in servers
where s.Name == serverName
select s;
Run Code Online (Sandbox Code Playgroud)
我收到错误:
无法找到源类型"MyNamespace.ServerDetails"的查询模式的实现.'哪里'找不到.
在ServerElement具有两个属性:
public class ServerElement : ConfigurationElement
{
[ConfigurationProperty("ip")]
public string IP
{
get { return (string)base["ip"]; }
set { base["ip"] = value; }
}
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
ServerDetails
public sealed class ServerDetails : ConfigurationSection
{ …Run Code Online (Sandbox Code Playgroud) 我最近写了一个相当大的自定义配置组.我很好奇是否可以通过以下方式将此配置移动到单独的文件中:
<configuration>
<configSections>
<sectionGroup name="MyCustomGroup">
<section name="MyCustomSection"/>
</sectionGroup>
</configSections>
<MyCustomGroup file="alt.config" />
</configuration>
Run Code Online (Sandbox Code Playgroud)
这与appSettings的文件属性类似.我意识到很可能需要为我的自定义部分处理程序创建一个ConfigurationPropertyAttribute,但是我在找到这方面的任何示例或方向方面都没有成功.
我是c#中配置部分的初学者,
我想在配置文件中创建自定义部分.我在谷歌搜索后尝试的是如下
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="MyCustomSections">
<section name="CustomSection" type="CustomSectionTest.CustomSection,CustomSection"/>
</sectionGroup>
</configSections>
<MyCustomSections>
<CustomSection key="Default"/>
</MyCustomSections>
</configuration>
Run Code Online (Sandbox Code Playgroud)
CustomSection.cs
namespace CustomSectionTest
{
public class CustomSection : ConfigurationSection
{
[ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public String Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用此代码检索Section时,我收到一条错误说配置错误.
var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection");
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢.
编辑
我最终需要的是什么
<CustomConfigSettings>
<Setting id="1">
<add key="Name" value="N"/>
<add key="Type" …Run Code Online (Sandbox Code Playgroud) 我如何编写自定义,ConfigurationSection以便它既是段处理程序又是配置元素集合?
通常,您有一个继承自的类,该类具有继承自ConfigurationSection的类型的属性,ConfigurationElementCollection然后返回继承自的类型的集合的元素ConfigurationElement.要配置它,您需要看起来像这样的XML:
<customSection>
<collection>
<element name="A" />
<element name="B" />
<element name="C" />
</collection>
</customSection>
Run Code Online (Sandbox Code Playgroud)
我想删除<collection>节点,只需:
<customSection>
<element name="A" />
<element name="B" />
<element name="C" />
<customSection>
Run Code Online (Sandbox Code Playgroud) 是否可以使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
如何从App.config中读取此自定义配置?
<root name="myRoot" type="rootType">
<element name="myName" type="myType" />
<element name="hisName" type="hisType" />
<element name="yourName" type="yourType" />
</root>
Run Code Online (Sandbox Code Playgroud)
而不是这个:
<root name="myRoot" type="rootType">
<elements>
<element name="myName" type="myType" />
<element name="hisName" type="hisType" />
<element name="yourName" type="yourType" />
</elements>
</root>
Run Code Online (Sandbox Code Playgroud) 我在App.config中有以下针对.NET 3.5 Windows服务的位:
<configSections>
<section name="ConfigurationServiceSection" type="SomeApp.Framework.Configuration.ConfigurationServiceSection, SomeApp.Framework"/>
</configSections>
<ConfigurationServiceSection configSource="ConfigSections\configurationServiceSection.config" />
Run Code Online (Sandbox Code Playgroud)
我在configurationServiceSection.config中有这个:
<ConfigurationServiceSection>
<ConfigurationServices>
<ConfigurationService name="LocalConfig" host="localhost" port="40001" location="LON"/>
</ConfigurationServices>
</ConfigurationServiceSection>
Run Code Online (Sandbox Code Playgroud)
这是代码:
using System.Configuration;
namespace SomeApp.Framework.Configuration
{
public sealed class ConfigurationServiceSection : ConfigurationSection
{
[ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)]
[ConfigurationCollection(typeof(ConfigurationServices))]
public ConfigurationServices ConfigurationServices
{
get
{
return (ConfigurationServices)base["ConfigurationServices"];
}
}
}
public sealed class ConfigurationServices : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ConfigurationService();
}
protected override object GetElementKey(ConfigurationElement element)
{
ConfigurationService configService = (ConfigurationService) …Run Code Online (Sandbox Code Playgroud) 我有一个关于从自定义配置文件加载属性的问题.我已经尝试了两种不同的方法来加载我的oauth.properties文件,但我无法工作,所以我希望有人可以帮助我.
我尝试的第一种方法是将文件添加到conf目录并因此引用它:
String oauthPropertiesFile = ClassLoader.getSystemResource("oauth.properties").getFile();
Run Code Online (Sandbox Code Playgroud)
但那刚刚回来了NULL.
我尝试的第二种方法是添加:
@include.oauthProperties = oauth.properties
Run Code Online (Sandbox Code Playgroud)
到application.conf文件,然后在我的控制器中引用它,如:
String clientId = oauthProperties.clientId;
Run Code Online (Sandbox Code Playgroud)
但是这不会编译.
任何人都可以解释我在这里做错了什么吗?
我需要以下列方式进行自定义配置
<root>
<group>
<groupitem>
<property1/>
<property2/>
<property3/>
<property4/>
</groupitem>
<group>
</root>
Run Code Online (Sandbox Code Playgroud)
我没有找到任何如何做到这一点的例子.
以下非常接近,
但我仍然坚持定义与groupitem相对应的类.如果我改变property1,property2从元素属性,很容易定义.
但如果这些属性具有嵌套属性,那将会产生问题.
问题是,如何以上面定义的方式定义嵌套层次结构?
我在web.config中有这个:
<MySection>
<Setting1 Value="10" />
<Setting2 Value="20" />
<Setting3 Value="30" />
<Setting4 Value="40" />
</MySection>
Run Code Online (Sandbox Code Playgroud)
我想阅读所有部分"MySection"并获取所有值List<string>(例如:"10","20","30")
谢谢,
.net c# configuration configurationsection custom-configuration