So this a new one for me.
I'm trying to define a ConfigurationSection class in my class library that pulls from App.Config in my WinForms app. I've never done this before but from following examples this is where I've got to.
app.config in my WinForms app
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ReportEngineConfig" type="Optima.ReportEngine.ReportEngineConfig" allowDefinition="Everywhere" allowLocation="true"/>
</configSections>
<ReportEngineConfig>
<ReportObjectVariableRegEx value="test" ></ReportObjectVariableRegEx>
</ReportEngineConfig>
</configuration>
And my ConfigurationSection class in my seperate class library.
using System.Configuration;
namespace Optima.ReportEngine
{
public class ReportEngineConfig : ConfigurationSection
{
[ConfigurationProperty("ReportObjectVariableRegEx")]
public string ReportObjectVariableRegEx
{
get
{
return (string)this["value"];
}
}
}
}
So any chance anyone can point out where I've gone wrong
Thanks!
Your type tag needs to reference the assembly name, not just the type name:
type="Optima.ReportEngine.ReportEngineConfig, Optima.ReportEngineAssembly"
Where the section after the comma is the name of the assembly containing ReportEngineConfig. You'll also have to make sure the application that is using this app.config has referenced the same assembly containing ReportEngineConfig.
Also you can get rid of the allowDefinition and allowLocation tags... you've put the defaults in.
Related
I have created a Console application project(proj name: ConsoleApp) with .Net core 3.1. I have added a specflow feature file where in I have defined my test scenario and also a class file which has my test case
[Given(#"I have started the application")]
public void GivenIHaveStartedTheApplication()
{
TestClass testClass1 = new TestClass();
testClass1.ReadConfigValues();
}
Also, I have added an App.config in the same ConsoleApp Project which looks like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="User" value="Admin" />
<add key="Password" value="nopassword" />
</appSettings>
</configuration>
TestClass is a C# class file which is residing in a different Class Library project(proj name: TestProject) with .Net Framework 4.7. This project has been referenced by ConsoleApp
Code for TestClass is as below
public class TestClass
{
public void ReadConfigValues()
{
string user = ConfigurationManager.AppSettings["User"];
string password = ConfigurationManager.AppSettings["Password"];
}
}
The issue I'm facing here is that the values for both user and password are returned as null. Basically ConfigurationManager.AppSettings has a count equal to zero.
Could anyone point out if I'm missing anything?
I am trying to access one of the section of my 'web.config' file using below code
public static string XMLCheck
{
get
{
var section = (Hashtable)ConfigurationManager.GetSection("Default.Framework");
return (string)section["ConnectionString"];
}
}
but getting execption as Unable to cast object of type 'System.Configuration.ConfigXmlElement' to type 'System.Collections.Hashtable' What's wrong here? How to correct
?
Update
<Resources>
<Resource Uri="resource:Default:CrossDomain" Version="1.0" Id="8ae96c54" IsEnabled="True" Handler="handler:Default:Framework:Resources:Data:Oracle">
<Properties>
<Property Name="ConnectionString" Value="Data Source=TESTDB;User Id=TESTUSR;password=TESTPWD;Persist Security Info=false"/>
</Properties>
</Resource>
</Resources>
Verify that the configSections entry in your web.config is a DictionarySectionHandler:
<configuration>
<configSections>
<section name="Default.Framework" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<configuration>
From your updated code, it looks like you're using a library or framework that defines a custom XML structure for its config section. Normally, you would rely on this library to expose the config settings through its properties. If you really want to parse the XML, you could use XPath like the below:
public static string XMLCheck
{
get
{
var section = (XmlElement)ConfigurationManager.GetSection("Default.Framework");
var connectionString = section.SelectSingleNode(#"
descendant::Resource[#Uri='resource:Default:CrossDomain']
/Properties
/Property[#Name='ConnectionString']
/#Value");
return connectionString.Value;
}
}
I have an App.config of the form
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Custom.Config" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<kCura.Config configSource="Custom.config" />
</configuration>
With a custom configuration file Custom.config of the form
<?xml version="1.0" encoding="utf-8"?>
<Custom.Config>
<add key="foo" value="bar" />
</Custom.Config>
I've inherited this code, and there is no existing ConfigurationSection or ConfigurationSectionHandler associated with this Custom.Config section.
I can access the value of foo just fine with
DirectCast(System.Configuration.ConfigurationManager.GetSection("Custom.Config"), System.Collections.IDictionary)
But now I would like to update this value, programatically (for testing purposes). Is this possible?
I've read the following, and I'm still stumped; they seem to imply that this namespace is only for reading values, not full CRUD:
How to: Create Custom Configuration Sections Using ConfigurationSection
How to: Create Custom Configuration Sections Using IConfigurationSectionHandler
You'll have to modify the Custom.config file on disk by using XDocument then you'll need to call RefreshSection on the ConfigurationManager
This will then refresh the named section so the next time that it is retrieved it will be re-read from disk.
I have an MVC project and am trying to store my API keys in a separate config file which I will ignore when pushing the code to Git. According to MSDN I should be able to store them in an App.config like like so
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="APIKey" value="APIKeyValue" />
</appSettings>
</configuration>
I should then be able to read from the file by creating a method in a model
public class KeyTest
{
public string KeyTestCall()
{
string testkey = ConfigurationManager.AppSettings.Get("APIKey");
return testkey;
}
}
and then invoke the method in my controller to assign the value from my App.config file (just so I know I'm getting the value).
public void Testing()
{
KeyTest k = new KeyTest();
ViewBag.x = k;
}
At no point will the code break for a breakpoint, the build will succeed and I can't tell if I'm getting the value or not. Any help is much appreciated. Thanks!
For a web application such as an MVC app, it's a Web.config file, not an App.config
In addition to above (re: web.config vs app.config) if you want to remove "secrets" from source control, this is one way to do it:
In web.config
<?xml version="1.0" encoding="utf-8"?>
<cofiguration>
....
<appSettings file="AppKeys.config">
<add key="SomeOtherSettingThatHasNoSecrets" value="foo" />
...
Then in a separte AppKeys.config file (you can name this whatever.config, sample as named in the above), that you don't add to Git/source control:
<appSettings>
<add key="SomeSecretKey" value="the secret" />
...
Note that AppKeys.config doesn't have an XML declaration.
Hth.
I have a solution with three projects.
Project1 (Windows Application)
Project2 (Windows Application)
Project3 (Class Library)
Project1 and Project2 have their own app.config files with appSettings. Now I want the add some appSettings in the Project3. But I don't want the write the new settings in both app.config files. I want a new configfile/section for my shared solution.
How can I create my own config file in Project3 and reference from Project1 and Project2?
Have you tried:
<appSettings configSource="shared.config">
</appSettings>
Then in shared.config
<?xml version="1.0"?>
<appSettings>
<add key="foo" value="bar" />
</appSettings>
Here is a working example. It's not perfect, but it's working:
Create a share.config in Project3
<?xml version="1.0" encoding="utf-8" ?>
<shareConfig
config1="value1">
</shareConfig>
Create a ShareConfig.cs file in your Project3
public class ShareConfig : ConfigurationSection
{
[ConfigurationProperty("config1", IsRequired = true)]
public string Config1
{
get { return (string)base["config1"]; }
set { base["config1"] = value; }
}
}
Add the config File as a Link the Project1 and Project2
Add the following code in your app.config (Project1 and Project2):
<configSections>
<section name="shareconfig" type="[classname], [projectname], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</configSections>
<shareconfig configSource="App.Share.config"/>
Now you can use your new config Section like this:
ShareConfig config = (ShareConfig )ConfigurationManager.GetSection("shareConfig ");