Getting ConnectionString of a BizTalk config file from a C# Application - c#

I have a BizTalk config file that I want to use in my C# application. I'd like to get the connection string from the BizTalk config. Is there a way to do this? Simply put, I want to read a connection string from an external config file.
What I'm currently using in my C# app config is:
<configuration>
<appSettings>
<add key="foo" value="blah;"/>
<add key="foo" value="blah;"/>
</appSettings>
</configuration>
I get the keys by using this code:
connectionString = ConfigurationManager.AppSettings[configKey];
Thanks.

From an external .config file, as in not the current .exe's config file, no.
System.Configuration will always refer to the local .config.
To access another .exe's .config file, you have to treat it as just an Xml file with System.Xml.

Related

Centralised Connection String for multiple application asp.net

I have a web server with multiple application running. All the application have their web.config file. If the database password changes due to Policy I have to manually change the password in each of web.config files in the app setting section.
I was reading about the connection string setting in machine.config file.
Now my question is if I put connection string in appsetting section of machine.config with name ConnectionString and same in my web.config file will it overwrite the machine.config file values.
In my machine.config following is the setting
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
similarly in my web.config file
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
And I get the value in my code as below
string conString=ConfigurationManager.AppSettings["ConnectionString"];
will I get the overloaded value?
What's going to help you out here is to store your connection string(s) in .config file and then reference them either using the file="" attribute or the configSource="" attribute.
Here's an excellent question and answer that talks about the differences between the two and shows you how to implement them:
ASP.NET web.config: configSource vs. file attributes

app.config file C# winforms preserve the connection string

I have an app.config file in Winforms application that holds a connection string. This is to go out to multiple tenant (clients) as a separate file. These clients have different database sources. This config file also holds other version information such as EF, Telerik reporting etc...
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
and
<section name="Telerik.Reporting"
type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting, Version=8.1.14.804, Culture=neutral, PublicKeyToken=a9d7983dfcc261be"
allowLocation="true" allowDefinition="Everywhere" />
The problem I have is when we have an updated version of EF or Telerik reporting with our application and we deploy (auto-deploy) this we need to overwrite the app.config file in the client directory to update the versions in the client config file. They then lose their connection setting and I do not want the client to have to go and re-enter it.
My question:
Is there a best practice to overcome this issue? Should I hold the connection string somewhere else?
Yep, the best thing to do is to move your connection strings section to an another config file and reference that file within your app.config.
For example create a new file called connectionStrings.config:
<connectionStrings>
<add name="Default" connectionString="[client_connection_string] "/>
</connectionStrings>
And in your app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
</configuration>
A full example can be found here.
Use an external configuration file that is referenced from the application config file. E.g. include this section in your config file.
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
The external config file is described http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
Note that storing connection settings in plaintext on a workstation is still a bad idea.
Using Windows registry for stuff like this is a definite no-no these days.
you can try to hold all connection data that you need in separate xml file so it dont get overwrite when you preform a deploy of updated version.

ConfigurationManager does not point to correct app.config

I am trying to use ConfigurationManager to get the connection string from a project call FileShareAccessLibrary.
This is the code I am writting in order to do this:
ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"].ConnectionString
This is the content of app.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections></configSections>
<connectionStrings>
<add name="FileShareAccessLibrary" connectionString="......"
providerName="System.Data.SqlClient" />
<add name="FileShareAccessLibrary.Properties.Settings"
connectionString="..."
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
When I run my app I get a NullReferenceException because ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"] returns null.
While debugging I noticed that none of the two connection strings are stored inside ConfigurationManager.ConnectionStrings so I figured that the ConfigurationManager is pointing to another file.
In my project I have not other app.config file.
Is there something I am doing wrong here?
Why is ConfigurationManager not getting my connection string?
If your FileShareAccessLibrary project is a class library rather than a windows forms application or console application then you will need to move the connection strings (and app settings if you have any) from the FileShareAccessLibrary config file to the config file of the application(s) that reference the FileShareAccessLibrary dll.

Hiding private details from open source projects

I have a .net github project that is basically a wrapper around a web API. In the test project, I am calling to the API using an API key. I need to keep this key private, how do I accomplish this in a visual studio project?
In some other projects, like python, I can have git ignore the file (config.py) and use something like config.example.py. But in visual studio's case, the project will not compile because of the missing file Config.cs. What is the proper way to solve this? I'm thinking of using this same method of ignoring the file and have them execute a build script that should rename Config.example.cs to Config.cs?
This is the perfect for .config files. Depending on whether its a web or console application, you will have a web.config or app.config file in your project.
You can use the appSettings section to store your API key.
To make things even easier, you can actually have this section read from another file, ie: specialappsettings.config and then just ignore that single file from your repository.
Modify your web.config (or app.config):
<configuration>
<appSettings file="specialappsettings.config">
</appSettings>
<system.web>
<!-- standard web settings go here -->
</system.web>
</configuration>
Create a new specialappsettings.config file:
<appSettings>
<add key="APIKey" value="YourApiKeyValue" />
<add key="AnotherKey" value="AnotherValue" />
</appSettings>
This can be accessed in your code via:
var apiKey = ConfigurationManager.AppSettings["APIKey"];
Notes:
You can keep your settings within the original web.config file as
well but this lets you ignore just the specific settings file from
your git repository without affecting the rest of the project's
necessary configuration details.
The same "key" can be saved in
either file however the external file will override the original
web.config file value.
You are probably looking for the App.config file for a project. It will be copied to <application>.exe.config when you compile it. Users can edit that config file as needed.
In that config file, you can add your API keys:
<configuration>
<appSettings>
<add key="APIKey" value="12345"/>
</appSettings>
</configuration>
Then you can access it from your code using ConfigurationManager.AppSettings:
string apiKey = ConfigurationManager.AppSettings["APIKey"];
One option is to use .config files instead of having secret keys hardcoded in sources.
More info Using Settings in C# and step-by-step guide
<configuration>
<appSettings>
<add key="SecretKey" value="0" />
</appSettings>
</configuration>
var secretKey = ConfigurationManager.AppSettings.Get("SecretKey");
Perhaps you can store the key outside of the Config.cs file and load it at run time.
Bonus, other people using your code won't have to recompile the project to change to their API key.

Is it possible to read a web.configuration file and add some tags in another web.config file?

Let us consider a web.config file deployed from website....
let us take another project say console application or some class file then is it possible to read that web.configuration file and add some tags in that web.config file
i just want to add these lines in to it
<system.webServer>
<modules>
<add name="sample" type="sample.class" />
</modules>
</system.webServer>
Waiting for your valuable comments
Updated: Read this Article and download source code, it may help you to read/write new section in config files.
To read the AppSettings from the Web.Config file you can use the ConfigurationSettings or ConfigurationManager Class.
It works like the following
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
ConnectionInfo is in the Web.config file under the AppSettings like
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
and to write in the web.config AppSettings section.. Let's say you have a key 'SiteName' in your appsettings section of your config file... you can update it like that..
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
config.AppSettings.Settings["SiteName"].Value = "New Site Name Value";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

Categories