我正在尝试学习如何使用ConfigurationSection类.我曾经使用IConfigurationSectionHandler,但发布它已被折旧.因此,作为一个好孩子,我正在尝试"正确"的方式.我的问题是它总是返回null.
我有一个控制台应用程序和DLL.
class Program
{
static void Main(string[] args)
{
StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();
string value = section.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
app配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ConfigSectionGroup">
<section name="ConfigSection" type="Controller.StandardConfigSectionHandler, Controller" />
</sectionGroup>
</configSections>
<ConfigSectionGroup>
<ConfigSection>
<test value="1" />
</ConfigSection>
</ConfigSectionGroup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
DLL中的section处理程序:
namespace Controller
{
public class StandardConfigSectionHandler : ConfigurationSection
{
private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";
public static StandardConfigSectionHandler GetConfiguration()
{
object section = ConfigurationManager.GetSection(ConfigPath);
return section as StandardWcfConfigSectionHandler;
}
[ConfigurationProperty("value")]
public string Value
{
get …Run Code Online (Sandbox Code Playgroud) 我想在程序中读/写(并保存)应用程序的配置文件
app.config是这样的:
<configuration>
<configSections>
<section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
</configSections>
<AdWordsApi>
<add key="LogPath" value=".\Logs\"/>
...
</AdWordsApi>
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我使用ConfigurationManager.GetSection读取app.config时,它可以工作:
var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);
Run Code Online (Sandbox Code Playgroud)
但是当我使用ConfigurationManager.OpenExeConfiguration时:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");
Console.WriteLine(section["LogPath"]);
Run Code Online (Sandbox Code Playgroud)
我总是得到这个错误:
由于其保护级别,'System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]'无法访问
但据我所知,GetSection无法在程序运行时保存配置,就像我在开头说的那样:我想在程序运行时保存配置,所以我必须使用OpenExeConfiguration.
我已经google了很长时间,我发现使用AppSettings,但我使用的是自定义部分..
任何人都可以解释为什么会出现"ConfigurationProperty无法访问"错误?谢谢
编辑:
我已复制本地的系统和System.Configuration至真
.net c# configurationmanager app-config configurationsection
我有一个包含以下ConfigurationSection的类:
namespace DummyConsole {
class TestingComponentSettings: ConfigurationSection {
[ConfigurationProperty("waitForTimeSeconds", IsRequired=true)]
[IntegerValidator(MinValue = 1, MaxValue = 100, ExcludeRange = false)]
public int WaitForTimeSeconds
{
get { return (int)this["waitForTimeSeconds"]; }
set { this["waitForTimeSeconds"] = value; }
}
[ConfigurationProperty("loginPage", IsRequired = true, IsKey=false)]
public string LoginPage
{
get { return (string)this["loginPage"]; }
set { this["loginPage"] = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在.config文件中有以下内容:
<configSections>
<section name="TestingComponentSettings"
type="DummyConsole.TestingComponentSettings, DummyConsole"/>
</configSections>
<TestingComponentSettings waitForTimeSeconds="20" loginPage="myPage" />
Run Code Online (Sandbox Code Playgroud)
当我然后尝试使用此配置部分时,我收到以下错误:
var Testing = ConfigurationManager.GetSection("TestingComponentSettings")
as TestingComponentSettings;
Run Code Online (Sandbox Code Playgroud)
ConfigurationErrorsException未处理
属性"waitForTimeSeconds"的值无效.错误是:该值必须在1-100范围内. …
我正在编写一个配置系统,其中app.config文件是从分布在多个位置的各种配置片段动态构建的.该系统目前的工作原理如下:
我们想摆脱这种多AppDomain方法; 它增加了一层复杂性,特别是涉及非托管库和其他遗留代码时.
在迁移到一个AppDomain时,工作流程将更改为:
似乎ConfigurationManager在内存中缓存部分.因此,例如,如果我在步骤#3之前阅读AppSettings,我必须调用:ConfigurationManager.RefreshSection("appSettings");实际上,我必须确保刷新了引导程序使用的任何部分.
我能够迭代新配置文件中的所有配置部分并强制刷新它们,但是,这会强制配置管理器加载配置文件中引用的任何程序集.如果可能的话,我想推迟这个.如果有办法使ConfigurationManager当前在内存中的内容无效?
.net configuration configurationmanager configurationsection
如何在后代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
我有一个如下所示的 appsettings.json
{
"AppName": "my-app-service",
"MyModel": {
"MyList": [{
"CountryCode": "jp"
},{
"CountryCode": "us"
}]
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个 POCO 文件,(MyList.cs 被省略,它有一个字符串键countryCode)
public class MyModel
{
public IList<MyList> MyList;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将 MyModel 注入到我的代码中的任何位置,我该怎么做?我看到人们在他们的 setup.cs 中这样做
services.Configure<MyModel>(Configuration.GetSection("MyModel"));
Run Code Online (Sandbox Code Playgroud)
不管怎样,当我IOption<MyModel>在构造函数中使用代码时,我只是得到空值。我哪里错了?
我得到以下例外:"每个配置文件只能出现一次.请参阅帮助主题以了解异常."
我的配置文件如下所示:
<configSections>
<sectionGroup name="point.System">
<section name="singleInstanceCache"
type="xyz.Point.System.Configuration.SingleInstanceCache, Point.System" />
</sectionGroup>
<sectionGroup name="point.Services">
<sectionGroup name="xServices" type="xyz.Point.Messaging.PointServiceConfiguration.PointServices, Barcap.FIA.Point.Messaging">
<section name="xService"
type="xyz.Point.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" />
</sectionGroup>
</sectionGroup>
</configSections>
<point.Services>
<xServices>
<xService name="Service1" type="IService" >
<endpoints>
<endpoint aliasName="incoming" endpointName="Subscriber"/>
<endpoint aliasName="outgoing" endpointName="Publisher"/>
</endpoints>
</xService>
<xService name="BlobService" type="IPortfolioService" >
<endpoints>
<endpoint aliasName="incoming" endpointName="Subscriber"/>
<endpoint aliasName="outgoing" endpointName="Publisher"/>
</endpoints>
</xService>
</xServices>
</point.Services>
Run Code Online (Sandbox Code Playgroud)
这是我加载它的代码:
public class PointServices : ConfigurationSection
{
public static PointServices Get()
{
var t = (PointServices)ConfigurationManager.GetSection("point.Services/xServices");
return null;
}
//<summary>
//Declares a collection …Run Code Online (Sandbox Code Playgroud) 我刚刚滚动了一个自定义配置部分,为Intellisense创建了一个随附的架构文档,并根据Michael Stum对另一个类似问题的回答将其添加到Web.config的Schemas属性中。
不幸的是,可能是由于我是用有限的知识手工创建XSD的,所以Intellisense依赖于一个xmlns属性,该属性指向自定义config元素中存在的XSD文件的名称空间。但是,在运行项目时,我得到了无法识别的属性“ xmlns”。请注意,属性名称是区分大小写的错误。
我可能可以修改我的XSD文件以定义该xmlns元素的属性,但是我想知道这是否只是对较大问题的临时解决方案。我必须承认,我对XML名称空间没有很好的了解,因此这可能是让我直接了解一些事情的机会。
这是XSD文件的根xs:schema元素的属性:
<xs:schema id="awesomeConfig"
targetNamespace="http://awesome.com/schemas"
xmlns="http://awesome.com/schemas"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
在Web.config文件中创建元素时,Visual Studio 2008会自动追加:
<awesomeConfig xmlns="http://awesome.com/schemas"></awesomeConfig>
Run Code Online (Sandbox Code Playgroud)
因此,我是否完全误解了xs:schema属性的含义,还是正确的解决方案似乎看起来那么简单?
Concider以下配置部分:
<PluginSection>
<Plugins>
<Plugin name="Plug1">
<add MessageType="1" MessageSubType="1" Ringtone="chime.wav" Vibrate="1000:0:1"/>
<add MessageType="1" MessageSubType="2" Ringtone="chime2.wav" Vibrate="1000:0:1"/>
</Plugin>
<Plugin name="Plug2">
<add MessageType="1" MessageSubType="1" Ringtone="chime.wav"/>
<add MessageType="1" MessageSubType="2" Ringtone="chime2.wav"/>
<add MessageType="2" Ringtone="chime3.wav"/>
</Plugin>
</Plugins>
</PluginSection>
Run Code Online (Sandbox Code Playgroud)
我已经将其解析为ac#IConfigSectionHandler.现在我明白这个方法已被弃用,我应该使用ConfigurationSection,ConfigurationElements和ConfigurationElementCollections.我在网上(msdn和SO)理解这个例子没有问题.但到目前为止我见过的所有例子都使用了其中一个属性作为关键.我的元素作为插件名称,MessageType和MessageSubType的组合是唯一的.MessageSubType也是可选的.我可以使用推荐的类解析看起来像这样的配置部分,或者我是否必须通过添加"虚拟"键来改变我的配置以适应ConfigurationClasses的制度?
我无法在启动时将我的自定义保存ConfigurationSection在我的web.config文件中.
我已经按照本教程进行了通用实现.我的单元测试通过了,我能够ConfigurationSection在单元测试中保存de !
在应用程序启动时,我想ClaimCollection基于控制器类名和所属方法名建立一个:
Startup.cs
public void Configuration(IAppBuilder app)
{
Config = new HttpConfiguration();
BuildUnityContainer();
BuildClaimCollection(); <----------------
ConfigureOAuth(app);
WebApiConfig.Register(Config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(Config);
}
public void BuildClaimCollection()
{
//config section
MethodClaimSection methodClaimSection = BaseConfigurationSection<MethodClaimSection>.Open();
var methods = GetClaimAuthorizedMethods();
foreach (var method in methods)
{
var claimValue = GetClaimValueFromMethodName(method);
var claim = new MethodClaimElement()
{
Type = "Method",
Value = claimValue
};
methodClaimSection.MethodClaims.Add(claim);
}
methodClaimSection.Save();
}
Run Code Online (Sandbox Code Playgroud)
BaseConfigurationSection.cs
public abstract class BaseConfigurationSection<T> : ConfigurationSection …Run Code Online (Sandbox Code Playgroud)