Chr*_*mes 157 c# configuration app-config configuration-files
我正在尝试在项目中实现自定义配置部分,并且我一直在遇到我不理解的异常.我希望有人能填补这里的空白.
我App.config看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ServicesSection" type="RT.Core.Config.ServicesConfigurationSectionHandler, RT.Core"/>
</configSections>
<ServicesSection type="RT.Core.Config.ServicesSection, RT.Core">
<Services>
<AddService Port="6996" ReportType="File" />
<AddService Port="7001" ReportType="Other" />
</Services>
</ServicesSection>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我有一个ServiceConfig像这样定义的元素:
public class ServiceConfig : ConfigurationElement
{
public ServiceConfig() {}
public ServiceConfig(int port, string reportType)
{
Port = port;
ReportType = reportType;
}
[ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)]
public int Port
{
get { return (int) this["Port"]; }
set { this["Port"] = value; }
}
[ConfigurationProperty("ReportType", DefaultValue = "File", IsRequired = true, IsKey = false)]
public string ReportType
{
get { return (string) this["ReportType"]; }
set { this["ReportType"] = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
我有这样的ServiceCollection定义:
public class ServiceCollection : ConfigurationElementCollection
{
public ServiceCollection()
{
Console.WriteLine("ServiceCollection Constructor");
}
public ServiceConfig this[int index]
{
get { return (ServiceConfig)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(ServiceConfig serviceConfig)
{
BaseAdd(serviceConfig);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new ServiceConfig();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServiceConfig) element).Port;
}
public void Remove(ServiceConfig serviceConfig)
{
BaseRemove(serviceConfig.Port);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
}
Run Code Online (Sandbox Code Playgroud)
我缺少的部分是为处理程序做什么.最初,我尝试实现IConfigurationSectionHandler但发现了两件事:
我现在完全迷失了该做什么,所以我可以从配置中读取我的数据.请帮忙!
Rus*_*ure 182
之前的答案是正确的,但我也会给你所有的代码.
您的app.config应如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ServicesSection" type="RT.Core.Config.ServiceConfigurationSection, RT.Core"/>
</configSections>
<ServicesSection>
<Services>
<add Port="6996" ReportType="File" />
<add Port="7001" ReportType="Other" />
</Services>
</ServicesSection>
</configuration>
Run Code Online (Sandbox Code Playgroud)
你ServiceConfig和ServiceCollection班级保持不变.
你需要一个新的课程:
public class ServiceConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Services", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ServiceCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public ServiceCollection Services
{
get
{
return (ServiceCollection)base["Services"];
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题.要消费它你可以使用:
ServiceConfigurationSection serviceConfigSection =
ConfigurationManager.GetSection("ServicesSection") as ServiceConfigurationSection;
ServiceConfig serviceConfig = serviceConfigSection.Services[0];
Run Code Online (Sandbox Code Playgroud)
Mub*_*har 81
如果您正在寻找如下的自定义配置部分
<CustomApplicationConfig>
<Credentials Username="itsme" Password="mypassword"/>
<PrimaryAgent Address="10.5.64.26" Port="3560"/>
<SecondaryAgent Address="10.5.64.7" Port="3570"/>
<Site Id="123" />
<Lanes>
<Lane Id="1" PointId="north" Direction="Entry"/>
<Lane Id="2" PointId="south" Direction="Exit"/>
</Lanes>
</CustomApplicationConfig>
Run Code Online (Sandbox Code Playgroud)
然后你可以使用我的配置部分实现,以便开始System.Configuration为你的项目添加程序集引用
查看我使用的每个嵌套元素,第一个是具有两个属性的Credentials,所以我们先添加它
凭证元素
public class CredentialsConfigElement : System.Configuration.ConfigurationElement
{
[ConfigurationProperty("Username")]
public string Username
{
get
{
return base["Username"] as string;
}
}
[ConfigurationProperty("Password")]
public string Password
{
get
{
return base["Password"] as string;
}
}
}
Run Code Online (Sandbox Code Playgroud)
PrimaryAgent和SecondaryAgent
两者都具有相同的属性,看起来像是主服务器和故障转移的一组服务器的地址,因此您只需要为这两个服务器创建一个元素类,如下所示
public class ServerInfoConfigElement : ConfigurationElement
{
[ConfigurationProperty("Address")]
public string Address
{
get
{
return base["Address"] as string;
}
}
[ConfigurationProperty("Port")]
public int? Port
{
get
{
return base["Port"] as int?;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将在本文稍后解释如何在一个类中使用两个不同的元素,让我们跳过SiteId,因为它没有区别.您只需要创建一个与上面相同的类,只使用一个属性.让我们看看如何实现Lanes集合
它分为两部分,首先你必须创建一个元素实现类,然后你必须创建集合元素类
LaneConfigElement
public class LaneConfigElement : ConfigurationElement
{
[ConfigurationProperty("Id")]
public string Id
{
get
{
return base["Id"] as string;
}
}
[ConfigurationProperty("PointId")]
public string PointId
{
get
{
return base["PointId"] as string;
}
}
[ConfigurationProperty("Direction")]
public Direction? Direction
{
get
{
return base["Direction"] as Direction?;
}
}
}
public enum Direction
{
Entry,
Exit
}
Run Code Online (Sandbox Code Playgroud)
您可以注意到一个属性LanElement是枚举,如果您尝试使用未在枚举应用程序中定义的配置中的任何其他值,则会System.Configuration.ConfigurationErrorsException启动启动.好的,我们继续前往Collection Definition
[ConfigurationCollection(typeof(LaneConfigElement), AddItemName = "Lane", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class LaneConfigCollection : ConfigurationElementCollection
{
public LaneConfigElement this[int index]
{
get { return (LaneConfigElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(LaneConfigElement serviceConfig)
{
BaseAdd(serviceConfig);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new LaneConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((LaneConfigElement)element).Id;
}
public void Remove(LaneConfigElement serviceConfig)
{
BaseRemove(serviceConfig.Id);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(String name)
{
BaseRemove(name);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以注意到我已经设置了AddItemName = "Lane"你可以选择任何你喜欢的收藏条目项目,我更喜欢使用"添加"默认的一个,但我只是为了这篇文章改变它.
现在我们已经实现了所有嵌套元素,现在我们应该聚集一个必须实现的类中的所有元素 System.Configuration.ConfigurationSection
CustomApplicationConfigSection
public class CustomApplicationConfigSection : System.Configuration.ConfigurationSection
{
private static readonly ILog log = LogManager.GetLogger(typeof(CustomApplicationConfigSection));
public const string SECTION_NAME = "CustomApplicationConfig";
[ConfigurationProperty("Credentials")]
public CredentialsConfigElement Credentials
{
get
{
return base["Credentials"] as CredentialsConfigElement;
}
}
[ConfigurationProperty("PrimaryAgent")]
public ServerInfoConfigElement PrimaryAgent
{
get
{
return base["PrimaryAgent"] as ServerInfoConfigElement;
}
}
[ConfigurationProperty("SecondaryAgent")]
public ServerInfoConfigElement SecondaryAgent
{
get
{
return base["SecondaryAgent"] as ServerInfoConfigElement;
}
}
[ConfigurationProperty("Site")]
public SiteConfigElement Site
{
get
{
return base["Site"] as SiteConfigElement;
}
}
[ConfigurationProperty("Lanes")]
public LaneConfigCollection Lanes
{
get { return base["Lanes"] as LaneConfigCollection; }
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以看到我们有两个名称属性,PrimaryAgent并且SecondaryAgent两者都具有相同的类型,现在您可以轻松理解为什么我们只有一个针对这两个元素的实现类.
在app.config(或web.config)中使用这个新发明的配置部分之前,您只需告诉应用程序您已经发明了自己的配置部分并给予了一些尊重,为此您必须添加以下行在app.config中(可能在root标签启动后).
<configSections>
<section name="CustomApplicationConfig" type="MyNameSpace.CustomApplicationConfigSection, MyAssemblyName" />
</configSections>
Run Code Online (Sandbox Code Playgroud)
注意: MyAssemblyName应该没有.dll,例如,如果您的汇编文件名是myDll.dll,那么使用myDll而不是myDll.dll
要检索此配置,请在应用程序的任何位置使用以下代码行
CustomApplicationConfigSection config = System.Configuration.ConfigurationManager.GetSection(CustomApplicationConfigSection.SECTION_NAME) as CustomApplicationConfigSection;
Run Code Online (Sandbox Code Playgroud)
我希望上面的帖子可以帮助您开始使用一些复杂的自定义配置部分.
快乐编码:)
****编辑****要启用LINQ,LaneConfigCollection您必须实现IEnumerable<LaneConfigElement>
并添加以下实现 GetEnumerator
public new IEnumerator<LaneConfigElement> GetEnumerator()
{
int count = base.Count;
for (int i = 0; i < count; i++)
{
yield return base.BaseGet(i) as LaneConfigElement;
}
}
Run Code Online (Sandbox Code Playgroud)
对于仍然对产量如何真正起作用感到困惑的人们,请阅读这篇精彩的文章
从上面的文章中得到的两个关键点是
它并没有真正结束方法的执行.yield return暂停方法执行,并在下次调用它时(对于下一个枚举值),该方法将从上一次yield return调用继续执行.我觉得这听起来有点令人困惑...... (Shay Friedman)
Yield不是.Net运行时的功能.它只是一个C#语言特性,可以通过C#编译器编译成简单的IL代码.(Lars Corneliussen)
Mzf*_*Mzf 46
这是配置集合的通用代码:
public class GenericConfigurationElementCollection<T> : ConfigurationElementCollection, IEnumerable<T> where T : ConfigurationElement, new()
{
List<T> _elements = new List<T>();
protected override ConfigurationElement CreateNewElement()
{
T newElement = new T();
_elements.Add(newElement);
return newElement;
}
protected override object GetElementKey(ConfigurationElement element)
{
return _elements.Find(e => e.Equals(element));
}
public new IEnumerator<T> GetEnumerator()
{
return _elements.GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
有了之后GenericConfigurationElementCollection,你可以在config部分中简单地使用它(这是我的Dispatcher中的一个例子):
public class DispatcherConfigurationSection: ConfigurationSection
{
[ConfigurationProperty("maxRetry", IsRequired = false, DefaultValue = 5)]
public int MaxRetry
{
get
{
return (int)this["maxRetry"];
}
set
{
this["maxRetry"] = value;
}
}
[ConfigurationProperty("eventsDispatches", IsRequired = true)]
[ConfigurationCollection(typeof(EventsDispatchConfigurationElement), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public GenericConfigurationElementCollection<EventsDispatchConfigurationElement> EventsDispatches
{
get { return (GenericConfigurationElementCollection<EventsDispatchConfigurationElement>)this["eventsDispatches"]; }
}
}
Run Code Online (Sandbox Code Playgroud)
配置元素配置在这里:
public class EventsDispatchConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string) this["name"];
}
set
{
this["name"] = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
配置文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<dispatcherConfigurationSection>
<eventsDispatches>
<add name="Log" ></add>
<add name="Notification" ></add>
<add name="tester" ></add>
</eventsDispatches>
</dispatcherConfigurationSection>
Run Code Online (Sandbox Code Playgroud)
希望它有所帮助!
fea*_*net 25
对于那些不想手动编写所有配置样板的人来说,这是一个更容易的选择......
1)从NuGet 安装Nerdle.AutoConfig
2)定义您的ServiceConfig类型(具体类或只是一个接口,或者都可以)
public interface IServiceConfiguration
{
int Port { get; }
ReportType ReportType { get; }
}
Run Code Online (Sandbox Code Playgroud)
3)你需要一个类型来保存集合,例如
public interface IServiceCollectionConfiguration
{
IEnumerable<IServiceConfiguration> Services { get; }
}
Run Code Online (Sandbox Code Playgroud)
4)像这样添加配置部分(注意camelCase命名)
<configSections>
<section name="serviceCollection" type="Nerdle.AutoConfig.Section, Nerdle.AutoConfig"/>
</configSections>
<serviceCollection>
<services>
<service port="6996" reportType="File" />
<service port="7001" reportType="Other" />
</services>
</serviceCollection>
Run Code Online (Sandbox Code Playgroud)
5)使用AutoConfig进行映射
var services = AutoConfig.Map<IServiceCollectionConfiguration>();
Run Code Online (Sandbox Code Playgroud)
尝试从ConfigurationSection继承。Phil Haack的这篇博客文章有一个示例。
根据IConfigurationSectionHandler的文档确认:
在.NET Framework 2.0版及更高版本中,您必须改为从ConfigurationSection类派生以实现相关的配置节处理程序。
| 归档时间: |
|
| 查看次数: |
143428 次 |
| 最近记录: |