App.config returns null - c#

There are a lot of questions like this and I've checked them out all.
Firstly, I checked the app.config's path using
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
to make sure code looking for the right app.config file.
After making sure of that, I still dont know why ConfigurationManager.AppSettings["BaseURL"]; is returning null. Any ideas?
Edit:
<appSettings>
<add key="BaseURL" value="blabla" />
</appSettings>
Answer Edit: Next time do not be like me and overwork your brain. You may forget you are working at a Virtual Machine, not at your desktop. In short, this is another "looking at the wrong place" question.

Just try this
Use WebConfigurationManager instead of using ConfigurationManager
Example :
Place below code in your web.config
<appSettings>
<add key="BaseURL" value="blabla" />
</appSettings>
and
To get the value use the WebConfigurationManager like below
string base_url= WebConfigurationManager.AppSettings["BaseURL"].ToString();

Related

Problem calling a second .config for keys in C#

I need a second .config to manage alot of keys. I tried using
<configSections>
<section name="apiConnection" type="CustomConfig.apiConnectionSection, CustomConfig" />
</configSections>
<apiConnection configSource ="ApiConnection.config"/>
Where "ApiConnection.config" is my .config file to manage keys but this didn't work.
Then i tried the "file" property in appSettings.
<appSettings file="ApiConnection.config">
This didn't work either. I Tried with:
../ApiConnection.config
~/ApiConnection.config
But nothing...
Some ideas?
The program doesnt break, just not show me the keys when i try with the ConfigurationManager.
https://i.stack.imgur.com/6xHK2.png
<img src= https://i.stack.imgur.com/6xHK2.png/>
EDIT
My file is in root path (with web.config)
The file looks like
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="Secret" value="z8xyHkN3Eju2TS9u-4MXeI2AbZiuTsF7xYJcjIJ" />
<add key="Audience" value="keyforms"/>
</appSettings>
Ok I think I know what your problem is based on your last comment.
This code is creating a new configuration section called apiConnection.
<configSections>
<section name="apiConnection" type="CustomConfig.apiConnectionSection, CustomConfig" />
</configSections>
<apiConnection configSource ="ApiConnection.config"/>
This section's values will not be contained in app settings. So you won't be able to access it via
ConfigurationManager.AppSettings
You will need to access it in a different manner. Now the exact manner will depend on your implementation of CustomConfig.apiConnectionSection and CustomConfig. I would search your code to find the class that defines how this works.
This example shows how to pull values from a custom config section, SecureAppSettings that uses the NameValueCollection in the same manner as AppSettings. You will have to do some digging to figure out what Types you will need to utilize.
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings");
string userName = section["userName"];

Extend an element in App.config

We have an app.config we are using with Carbonator:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="carbonator" type="Crypton.Carbonator.Config.CarbonatorSection, Crypton.Carbonator"/>
</configSections>
<carbonator defaultCulture="en-US" logLevel="1" collectionInterval="1000" reportingInterval="1000" >
<statsd server="127.0.0.1" port="8125" />
<counters>
<add path="processor_information.pct_processor_time.total" category="Processor" counter="% Processor Time" instance="_Total" />
<add path="memory.available_MBytes" category="Memory" counter="Available MBytes" instance="" />
<add path="memory.pct_commited_bytes_in_use" category="Memory" counter="% Committed Bytes In Use" instance="" />
</counters>
</carbonator>
</configuration>
We want to allow users to configure their own custom counters in an external config file that we reference from the <counters> element. For example, we would like to allow the user config file to look like:
<add path="logical_disk.pct_free_space.C" category="LogicalDisk" counter="% Free Space" instance="C:" />
<add path="logical_disk.disk_read_bytes_per_sec.C" category="LogicalDisk" counter="Disk Read Bytes/sec" instance="C:" />
<add path="logical_disk.disk_write_bytes_per_sec.C" category="LogicalDisk" counter="Disk Write Bytes/sec" instance="C:" />
I don't even know if this is possible outside of an appConfig element, but any help is appreciated.
According to this answer it should be possible. Same way is also described in this article.
But I don't think it's a good idea for one reason - if a user makes a mistake in his configuration extension, it will prevent the application from executing since the application configuration became invalid.
I would rather use the configuration in the app.config file to provide default values and implement some user configuration myself. Is such case, you can use whatever configuration format you like, for example JSON, which would be also better (easier to create and edit) for users. In your application, you simply merge both configurations (app.config values are default values which will be overwritten by the user's configuration).

Set Queue Path in Web.config

I am using MSMQ in C#, how can i set queuepath in web.config?
Any suggestions?
Thanks
Add an appSetting key with the path of your Queque ?
<appSettings>
<add key="myquequepath" value="FormatName:DIRECT=OS:machinename\private$\MyQueue" />
</appSettings>
It was really simple i found solution.
<add key="MSMQName" value=".\private$\WebSiteEmails"/>

Get connectionstring from web.config

I use System.Configuration.ConfigurationManager.AppSettings["key1"] in settings.designer.cs file. It's working fine in the development but after I moved all the .dll files into production it is not working.
In web.config file I added app settings in development and production both. What is the problem?
Code from settings.designer.cs file
get
{
return WebConfigurationManager.AppSettings["ConnectionString"];
//return (AppSettings["ConnectionString"]);
//return ((string)(this["ConnectionString"]));
}
I tried all three return statements. 3rd return is working fine in both dev & prod but it is not rendering from web.config.
Code in web.config
<add key="ConnectionString" value="connection string values are given here">
Don't use WebConfigurationManager.
Use System.Configuration.ConfigurationManager.AppSettings["key"] instead to read key-value pair kept in Web.config, e.g.:
<configuration>
<appSetttings>
<add key="key1" value="value1" />
</appSetttings>
</configuration>
and System.Configuration.ConfigurationManager.ConnectionStrings["name"].ConnectionString to read connection string, e.g.:
<configuration>
<connectionStrings>
<add name="name" connectionString="value1" />
</connectionStrings>
</configuration>
You have to add configuration setting (connectionstring) to last execution program config file.

What is ServicePointManager.FindServicePoint used for?

Can someone explain what ServicePointManager.FindServicePoint is intended to be used for? I have been writing some code to work with proxies in C#, and have seen indicators that it might be useful in this respect, but can't see why or how. How is this class (ServicePointManager) or method (ServicePointManager.FindServicePoint) supposed to be used (or when)?
Thanks.
The ServicePointManager.FindServicePoint(...) method will help you get the ServicePoint object that you've been specified in the configuration file.
Let's say, this is your configuration file:
<configuration>
<system.net>
<connectionManagement>
<add address="http://www.contoso.com" maxconnection="2" />
<add address="192.168.1.2" maxconnection="4" />
<add address="*" maxconnection="1" />
</connectionManagement>
</system.net>
</configuration>
This code would retrieve the "http://www.microsoft.com" ServicePoint:
ServicePoint sp1 = ServicePointManager.FindServicePoint(new Uri("http://www.microsoft.com"));
You can read all about it here.

Categories

Resources