Create custom section in config file - c#

I am trying to create custom section in app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="BlogSettings" type="ConsoleApplication1.BlogSettings,
ConsoleApplication1" />
</configSections>
<BlogSettings
Price="10"
title="BLACKswastik" />
</configuration>
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string title = BlogSettings.Settings.Title;
Console.WriteLine(title);
Console.ReadKey();
}
}
public class BlogSettings : ConfigurationSection
{
private static BlogSettings settings
= ConfigurationManager.GetSection("BlogSettings") as BlogSettings;
public static BlogSettings Settings
{
get
{
return settings;
}
}
[ConfigurationProperty("Price"
, DefaultValue = 20
, IsRequired = false)]
[IntegerValidator(MinValue = 1
, MaxValue = 100)]
public int Price
{
get { return (int)this["Price"]; }
set { this["Price"] = value; }
}
[ConfigurationProperty("title"
, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!##$%^&*()[]{}/;’\"|\\"
, MinLength = 1
, MaxLength = 256)]
public string Title
{
get { return (string)this["title"]; }
set { this["title"] = value; }
}
}
}
but when I run this code I am getting this error:
The type initializer for 'ConsoleApplication1.BlogSettings' threw an
exception.
Please suggest me whats wrong I am doing.

You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.
Unraveling the mysteries of .NET 2.0 configuration
Decoding the mysteries of .NET 2.0 configuration
Cracking the mysteries of .NET 2.0 configuration
Highly recommended, well written and extremely helpful! This will give you a thorough understanding of the .NET configuration system.
Phil Haack also has a great blog post Three easy steps to a custom configuration section that will give you a quick head-start into building your own custom config sections.
To design your own custom section, there's also a handy tool (Visual Studio add-in) called Configuration Section Designer that will make it very easy and simple to create your own custom section and have it build up all the necessary code to handle that custom section.

move this out of the config section
<BlogSettings
Price="10"
title="BLACKswastik" />
You made a new config reference so it can be its own node now.

Related

Reading from app.config yields null results

My issue is as the title reads, when I attempt to get some app settings for a given key, I am always returned with null.
I have 2 projects, 1 .Net Core Class library and 1 .Net Core Test Solution. Both contain an identical app.config.
https://imgur.com/a/oD8FHPW.
Here is the solution explorer so you can see the project set up.
In my tests solution, I am trying to test the validity of my helper using this test method
[TestMethod]
public void TestSaveLocationHelper()
{
ConfigurationResponseMessage connectionString = ConfigurationHelper.GetSaveLocation();
Assert.IsTrue(connectionString.ResponseCode == ResponseCode.SUCCESS);
}
I wrote a helper method that calls the configurationManager.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
namespace Unity.Helpers
{
public static class ConfigurationHelper
{
public static ConfigurationResponseMessage GetSaveLocation()
{
string saveLocation = ConfigurationManager.AppSettings[Constants.SAVE_LOCATION];
if (!string.IsNullOrWhiteSpace(saveLocation))
return new ConfigurationResponseMessage()
{
ConfigString = saveLocation,
ResponseCode = ResponseCode.SUCCESS
};
return new ConfigurationResponseMessage()
{
ConfigString = string.Empty,
ResponseCode = ResponseCode.FAILURE
};
}
}
}
saveLcoation is always null
Constants.SAVE_LOCATION = 'SaveLocation'
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SaveLocation" value="D:/test.json"/>
</appSettings>
</configuration>
Not entirely sure what is wrong. Thanks for any help

How do I add custom ConfigurationSection to Assembly?

I've spent a few weeks trying to figure this out, this is a duplicate of a question I asked previously but did not get a response to, so I am refining the question here.
I've created a custom class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Configuration;
namespace mssql_gui
{
public class TestConfigSection : ConfigurationSection
{
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public TestConfigInstanceCollection Instances
{
get { return (TestConfigInstanceCollection)this[""]; }
set { this[""] = value; }
}
}
public class TestConfigInstanceCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new TestConfigInstanceElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TestConfigInstanceElement)element).Key;
}
}
public class TestConfigInstanceElement : ConfigurationElement
{
[ConfigurationProperty("key", IsKey = true, IsRequired = true)]
public string Key
{
get { return (string)base["key"]; }
set { base["key"] = value; }
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)base["value"]; }
set { base["value"] = value; }
}
}
}
I've implemented it:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="testSection" type="mssql_gui.TestConfigSection"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<appSettings>
<add key="Data Source" value="localhost\SQLEXPRESS"/>
<add key="Initial Catalog" value="(empty)"/>
<add key="Integrated Security" value="SSPI"/>
</appSettings>
<testSection>
<add key ="testKey" value="tesValue"/>
</testSection>
</configuration>
and I have tried to access it, I am getting:
An error occurred creating the configuration section handler for testSection: Could not load type 'mssql_gui.TestConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
I understand that in the type, I should be declare an assembly dll, but I'm confused about that...because in the official instructions by MS, it says to create a new class for the handler:
Create a public class that inherits from the
System.Configuration.ConfigurationSection class.
Add code to define the section's attributes and elements.
Adding the class (at least through the visual studio interface) creates a .cs file, not a .dll assembly file, so how to I add that custom class to an assembly file in order to reference it in the <configSections> part of app.config?
If I understand correctly, you have problem with resolving what actually your Assembly is, since you are only creating .cs files that determine types that this file hold.
Assembly (in maybe not so accurate shorcut) is just the project you have in your solution. It will get compiled into its seperate assembly - the .dll you mentioned - later on.
When you add class to any .cs file in given project, on compile it will be included in project's assembly.
By default, if you won't provide assembly for configSection where its corresponding type should be found, App.config defaults to System.Configuration assembly - that's where you get your error from, since you've declared your section in your own assembly (== project).
Right click in Visual Studio on your project that holds App.config file and choose Properties to check its Assembly name:
Then add this name to your App.config section declaration. In my example its ConsoleApp1, so I will add it to configuration accordingly:
<configSections>
<section name="testSection" type="mssql_gui.TestConfigSection, ConsoleApp1"/>
</configSections>
Ensure that the type attribute of the section element matches the
manifest of the assembly (ensure that you specify both the correct
namespace and type name).
You need to add the name of the assembly (where the type relies) to the type attribute:
You'll get the name of the assembly from the AssemblyInfo.cs within the project where TestConfigSection class is defined.
<section name="testSection" type="mssql_gui.TestConfigSection, ASSEMBLYNAME"/>
Example asuming your assembly names mssql_gui
<section name="testSection" type="mssql_gui.TestConfigSection, mssql_gui"/>
You read it like this:
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
TestConfigSection mySec = (TestConfigSection)config.Sections["testSection"];
See more details at MSDN
How to: Create Custom Configuration Sections Using ConfigurationSection

Best way of injecting application configuration

Well, I'm making my foray into this fantastic site with a question about the correct way to inject configuration settings into application components. So, the overview is : I have an application written in C# .Net 3.5. It consists of 3 assemblies - a Core, a Data and a Service. The data & service assemblies require settings retrieved from the app.config, which is done via a settings file, eg.
Code :
public static String RequestQueueConnectionString
{
get { return ConnectionSettings.Default.RequestQueueConnectionString; }
}
Config :
<applicationSettings>
<MyNamespace.Data.ConnectionSettings>
<setting name="RequestQueueConnectionString" serializeAs="String">
...
Now, the assemblies are all setup using StructureMap for IoC - which to my mind should provide the answer to what I am looking for, but I just can't quite see it!
IoC :
public static void ConfigureStructureMap(IContainer container)
{
container.Configure(x => ...
...
What I want to be able to do is to inject a configuration class already populated into the IoC container such that those settings are used for that assembly, NOT those specified in the settings file / app.config. So perhaps :
public static void ConfigureStructureMap(IContainer container, MyConfigClass config)
{
container.Configure(x => x.For<DataConfig>()
.Singleton()
.Use ???
...
I hope I have provided enough details here - forgive a newbie if I have not and please let me know what else would be helpful in answering this!
So, after a lot of searching and trial and error, I was presented with #default.kramer's link, which I duely followed! With a little bit of trial and error, again (best way in my opinion), I managed to get the solution I was looking for. Now, whilst you can follow the link (and I would highly suggest doing so), I am going to post the solution to my question as I implemented it. Hopefully this might help someone with a similar problem.
So, I now have my configuration setup class like so :
public static class DispatchConfiguration
{
public static void ConfigureStructureMap(IContainer container, IDispatchConfiguration dispatchConfig)
{
DispatchProcessBatchSize = dispatchConfig.DispatchProcessBatchSize;
ServiceIsActive = dispatchConfig.ServiceIsActive;
...
}
Now, before I was using a settings file to retrieve the configuration out of the app.config file. This was obviously good for ensuring I had flexibility in changing my config settings, but it left me with the problem of not being able to easily test those settings. Say 9/10 tests required the service to be active, but 1 test wanted to test "ServiceIsActive = false;", now I'm in trouble.
Now, however, I am able to inject the configuration from the test :
[Given(#"Config\.IsServiceActive returns false")]
public void GivenConfig_IsServiceActiveReturnsFalse()
{
var settings = new DispatchSettings
{
ServiceIsActive = false,
DispatchProcessBatchSize = 100,
UpdatedBy = "Unit Test"
};
DispatchConfiguration.ConfigureStructureMap(ObjectFactory.Container, settings);
}
And then in the real world I am able to get the settings from app.config :
public void Start(String[] args)
{
var dispatchConfig = this.GetDispatchConfiguration();
DispatchConfiguration.ConfigureStructureMap(ObjectFactory.Container, dispatchConfig);
...
}
private IDispatchConfiguration GetDispatchConfiguration()
{
var config = (DispatchSettings)ConfigurationManager.GetSection("DispatchSettings");
return config;
}
And then the actual config class looks like :
[XmlRoot(ElementName = "DispatchSettings", Namespace = "")]
public sealed class DispatchSettings : IDispatchConfiguration
{
public Int32 DispatchProcessBatchSize { get; set; }
public Boolean ServiceIsActive { get; set; }
...
}
For the sake of completeness the interface looks like so :
public interface IDispatchConfiguration
{
Int32 DispatchProcessBatchSize { get; }
Boolean ServiceIsActive { get; }
...
}
And finally, the config file looks like this :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DispatchSettings" type="MyNamespace.XmlConfigurator, MyNamespace.Core" />
</configSections>
<DispatchSettings type="MyNamespace.DispatchSettings, MyNamespace.Core">
<ServiceIsActive>True</ServiceIsActive>
<DispatchProcessBatchSize>100</DispatchProcessBatchSize>
</DispatchSettings>
Now, anyone with a keen eye will spot "MyNamespace.XmlConfigurator". I found this on one of my Google journeys, and the code allows you to deserialize an Xml config into a class of your desire (as shown in this example). So, to ensure you have the complete code to make this technique work, below is the code for the XmlConfigurator. I cannot remember where I came across it, but a big thanks to the person who wrote it!!
public sealed class XmlConfigurator : IConfigurationSectionHandler
{
public XmlConfigurator()
{
}
public object Create(object parent, object configContext, XmlNode section)
{
XPathNavigator navigator = null;
String typeName = null;
Type sectionType = null;
XmlSerializer xs = null;
XmlNodeReader reader = null;
try
{
Object settings = null;
if (section == null)
{
return settings;
}
navigator = section.CreateNavigator();
typeName = (string)navigator.Evaluate("string(#type)");
sectionType = Type.GetType(typeName);
xs = new XmlSerializer(sectionType);
reader = new XmlNodeReader(section);
settings = xs.Deserialize(reader);
return settings;
}
finally
{
xs = null;
}
}
}
And there you have it! I hope this allows anyone with a similiar issue to resolve it and is clear enough to follow!

.NET 4.0 application on network share causes SecurityException

Today I experienced a weird problem while trying to remotely debug an application built for the .NET 4.0 runtime.
The application resides on a network share and executed by a remote machine. However the application crashes each time during load because of a SecurityException raised by a permission demand in the System.Configuration.ConfigurationManager.GetSection() method. I have not checked if other permission demands in the base class library also cause a security exception but in all cases this shouldn't be happening with the new CLR.
The application is running in full trust (checked it while debugging and as usual this must be always true for intranet applications in CLR 4.0) so I am clueless how a permission demand can cause an exception in this case. When built against the 3.5 SP1 runtime (which first introduced full trust for network shared apps by default) everythings runs as expected.
I pasted the sample code below. Any help is greatly appreciated.
using System;
using System.Configuration;
namespace ConsoleApplication1
{
public sealed class AssetsSection : ConfigurationSection
{
private static readonly ConfigurationProperty s_propPath;
private static readonly ConfigurationPropertyCollection s_properties;
static AssetsSection()
{
s_propPath = new ConfigurationProperty("path", typeof(String));
s_properties = new ConfigurationPropertyCollection()
{
s_propPath
};
}
public static AssetsSection Get()
{
return (AssetsSection) ConfigurationManager.GetSection("test/assets");
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return s_properties;
}
}
public String Path
{
get
{
return (String) base[s_propPath];
}
set
{
base[s_propPath] = value;
}
}
}
class Program
{
static void Main(String[] args)
{
Console.WriteLine(AssetsSection.Get().Path);
Console.ReadLine();
}
}
}
And the App.config file;
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="test">
<section name="assets" type="ConsoleApplication1.AssetsSection, ConsoleApplication1"/>
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>
<test>
<assets path="..\Assets"/>
</test>
</configuration>
Try loading the configuration first and open your section on that:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AssetsSection configSection = (AssetsSection)config.GetSection("test/assets");
I ran into the same issue with .NET 4 and this works for me.
This is due to a known bug in .NET 4.0 when running the application from a network share.
The follow code fails with a SecurityException. Note that it only fails when you have defined a custom type for the section like in this example AssetsSection:
ConfigurationManager.GetSection("test/assets");
One fix is the solution suggestion by Timo to use a different API. Another solution is to apply the patch provided by Microsoft.
The bug and the related hotfix is filed under KB2580188.
If you add your own class to map the section like this:
[XmlRoot("Interface")]
public class MySectionClass
{
[XmlAttribute()]
public string MyAttr1
{
get;
set;
}
public string MyAttr2
{
get;
set;
}
}
You can use this code:
ConfigurationSection configSection =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).
GetSection("MySection");
XmlSerializer xs = new XmlSerializer(typeof(MySectionClass));
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(configSection.SectionInformation.GetRawXml());
XmlNodeReader xnr = new XmlNodeReader(xdoc.DocumentElement);
MySectionClass section = (MySectionClass)xs.Deserialize(xnr);
I'm speculating here, but I suspect it's your configuration file that's not trusted.
In your case, your configuration file is referencing a type ConsoleApplication1.AssetsSection that does not have a strong name that could be used as evidence.
Can you provide more details and the exact error message.

C# AppSettings: Is there a easy way to put a collection into <appSetting>

i tried
<appSettings >
<add key="List" value="1"/>
<add key="List" value="2"/>
<add key="List" value="3"/>
</appSettings >
and System.Configuration.ConfigurationManager.AppSettings.GetValues("List");
But i only get the last member .
How could i solve this easily?
I have dealt a similar issue and I did it with this code. Hope this helps in your problem.
In this case List (similar to my URLSection) will have a full configuration Section in web.config which you can get all values from this section then.
<configSections>
<section name="URLSection" type="A.WebConfigSection,A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>
<appSettings></appSettings>
<URLSection>
<urlCollection>
<add url="1" value="a"/>
<add url="2" value="b"/>
</urlCollection>
</URLSection>
I made three classes for this: ConfigElement, ConfigElementCollection, WebConfigSection.
ConfigElement
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace A
{
public class ConfigElement:System.Configuration.ConfigurationElement
{
[ConfigurationProperty("url",IsRequired=true) ]
public string url
{
get
{
return this["url"] as string;
}
}
[ConfigurationProperty("value", IsRequired = true)]
public string value
{
get
{
return this["value"] as string;
}
}
}
}
ConfigElementCollection
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace A
{
public class ConfigElementCollection:ConfigurationElementCollection
{
public ConfigElement this[int index]
{
get
{
return base.BaseGet(index) as ConfigElement;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new ConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ConfigElement)(element)).url;
}
}
}
WebConfigSection
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace A
{
public class WebConfigSection:ConfigurationSection
{
public WebConfigSection()
{
}
[ConfigurationProperty("urlCollection")]
public ConfigElementCollection allValues
{
get
{
return this["urlCollection"] as ConfigElementCollection;
}
}
public static WebConfigSection GetConfigSection()
{
return ConfigurationSettings.GetConfig("URLSection") as WebConfigSection;
}
}
}
foreach (string str in ConfigurationManager.AppSettings.AllKeys)
{
if (str.ToUpper().IndexOf("SOMESPECIAL") > -1) //the somespecial ones you want to add in
lstList.Add(ConfigurationManager.AppSettings[str]);
}
NinjaSettings does this out of the box.
In the package manager console
Install-Package NinjaSettings
You would declare your list as
<appSettings>
<add key="List" value="50,20,10,100"/>
</appSettings>
then create an Interface with a mapping for list to any ICollection or Array
public interface IAppSettings
{
List<int> List { get; }
}
then access your settings user the NinjaSettings wrapper. Generally you would wire this up using IOC, but the basic usage is
var settings = new NinjaSettings<IAppSettings>().Settings;
int total = 0;
for (var i in settings.List)
{
total+=i;
}
You'd likely be better off putting this information in a separate XML file and having a reference to that file in AppSettings. That would give you a lot more flexibility around how you retrieved the information and consumed it.
The only thing would be that you'd want to create a separate (static?) class for reading the XML in a similar fashion to the System.Configuration.ConfigurationManager.AppSettings class.
If, on the other hand, it HAD to be in your Web.Config file, I would suggest the only way to achieve this simply would be to have a [pipe/comma/semi-colon] delimited array in one "List" setting.
Haacked provides a concise approach to configuration settings. His approach uses one class deriving from ConfigurationSection. So for his blog example your app.config or web.config xml representation will look like this:
<configuration>
<configSections>
<section name="BlogSettings" type="Fully.Qualified.TypeName.BlogSettings,
AssemblyName" />
</configSections>
<BlogSettings frontPagePostCount="10" title="You’ve Been Haacked" />
</configuration>
This is worth a read:
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

Categories