Little embarrassed to ask, but since I never had to do it, here it goes. I have a config file that has connection strings for the app stored in this format.
<connectionStrings>
<clear />
<add name="MyConnectionStringName" connectionString="{here is a connection string}"/>
</connectionStrings>
I tried ConfigurationManager but it requires that the file has "configuration" root element. Is there something else in Configuration name space, or should I just use LinqToXml?
Update: I cannot change anything in the file, file is already in the use for years by some in home grown framework. All content that is in the file is shown above.
Update2: Second look reveals that this file is referenced from App.Config as
<connectionStrings configSource="Config\connectionstrings.config"/>
which allows me to use ConfigurationManager.ConnectionStrings.
There is more to a configuration file than just the connectionStrings section. If you are using Visual Studio, the easy way to fix your problem is to use the IDE to add a new Application Configuration file. The IDE will create a shell file for you, which you can then add your connectionStrings section to (within the configuration section).
By hand, at bare minimum, you need this:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<clear />
<add name="MyConnectionStringName" connectionString="{connection string}"/>
</connectionStrings>
</configuration>
What's wrong in
<Configuration>
<appSettings>
<add key="MyConnectionStringName" value="{here is a connection string}"/>
</appSettings>
</Configuration>
Well you could load it into an XmlDocument and the Load method.
Once you have it loaded you can use SelectNodes with an xpath query to get back the nodes you want.
Off the top of my head something like
string conn;
XmlDocument xdoc = new XmlDocument();
xdoc.Load("path_to_file");
var configs = xdoc.SelectNodes("ConnectionStrings/Add");
foreach(XmlNode n in configs)
{
conn = n.Attributes["connectionString"];
}
Try that and see. you may need to play around with the xpath, been a while since i wrote one.
Related
We have multiple WCF projects and we want place the all connection strings in one connectionStrings.config file and use it from there. For this I have tried the following:
connectionStrings.config file in parent folder of all WCF projects (path: D:\Projects\connectionStrings.config)
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
web.config file of each WCF Project (path: D:\Projects\SampleWCFProject\web.config)
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="..\connectionStrings.config"/>
</configuration>
But I get this error:
The configSource attribute is invalid.: The configSource '..\connectionStrings.config' is invalid. It must refer to a file in the same directory or in a subdirectory as the configuration file.
It works if we place the connectionStrings.config in same folder as the web.config or in a child folder. But that's not what we require.
Can anyone help us with this? Thanks in advance.
In my opinion, we could refer to this file by Adding Existing Item.
Then switch the “build action” to “content” and “copy to output directory” to “Always” in the property page of the referring file.
We could choose to add a file to the current project or a link to this file.
Finally, the file in other directory cannot work may be due to problems with read and write permissions to the file.
Feel free to let me know if there is anything I can help with.
Ok, I am using an external config file for my connection strings so each individual developer can have their own strings while developing. Normally, each dev has a different environment but we all publish to the same servers via web.release.config transforms.
However, when I publish from VS, it's not transforming from the web.release.config for the conn strings. I think it's because if you have the configSource attribute set to use an external config it ignores the transform.
Here's my web.config:
<connectionStrings configSource="userConn.config" />
And here's my userConn.config:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=XXXX;Initial Catalog=XXXX;user id=XXXX;password=XXXX;" />
<add name="ExtVariablesEntities"
providerName="System.Data.EntityClient" connectionString="metadata=res://*/Models.XXXX.csdl|res://*/Models.XXXX.ssdl|res://*/Models.ExtVariables.msl;provider=System.Data.SqlClient;provider connection string="data source=XXXX;initial catalog=XXXX;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework;"" />
</connectionStrings>
After publishing, opening the actual web.config that made it to the server, it still has:
<connectionStrings configSource="userConn.config" />
Is there a workaround for this? I've had this setup before I just don't remember what the trick is.
Just an FYI that I was able to solve this by following this article: https://jshowers.com/simple-web-config-transforms-for-configuration-elements-that-use-configsource-and-external-files/
You end up with 3 custom config files - whatever you want to call them for dev, test and prod lets say. Then you will have 3 web config files dev, test and prod (these are your web config transform files) where you simply say:
<appSettings xdt:Transform="Replace" configSource="path.to.custom.config.file.depending.on.env">
In an open source ASP.NET application I'm working on, I need to keep certain data in the configuration file private, while still keeping it easy for people to build and debug it on their own machine. This is data such as API keys, Mail Settings, etc..
How would I keep this data separate and out of the git repository while still allowing people to just pull and build without having to set up a bunch of stuff?
In your config file you can define configSource:
<configuration>
<appSettings configSource="filepath1.config" />
<connectionStrings configSource="filepath2.config" />
<!--etc-->
</configuration>
Put the configurations that you need to keep private in a separate config file, then exclude them in your .gitignore.
Keep in mind that this will ignore the whole section and overwrite it with the context you have in the referenced file.
You can also do Configuration Transform, which allows you to only overwrite a small set of variables in sections. For example:
In your main Web.config:
<configuration>
<appSettings>
<add key="Key1" value="Something I dont't Care"/>
<add key="Key2" value="Something dummy"/>
</appSettings>
</configuration>
And in your Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Key2" value="Something I want to keep secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In this case the "Key2" value that you want to keep private will be in a separate file, and you can exclude the Web.Release.config through .gitignore.
Also there's another approach that I never tried, which can also overwrite config using external file.
Is there a quick and easy way to set a connection string in Web.Config to be the active connection string?
I basically want to name my connection strings appropriately and then set one as active without having to switch out the names or re-compile my application.
Something like this:
<add name="Current" connectionString="{Local}"/>
<add name="Local" connectionString=[...]" />
<add name="RemoteOnMyServer" connectionString=[...]" />
<add name="RemoteAzure" connectionString=[...]" />
I don't think that is possible the way you asked. But you can move the connection strings block to a separate file and then control which FILE is the active one:
<connectionStrings configSource="LocalDb.config"/>
Then you can have separate config files:
LocalDb.config
RemoteOnMyServer.config
RemoteAzure.config
<etc>
Each one of there would hold something like this:
<?xml version="1.0"?>
<connectionStrings>
<add name="namedConnectionString" connectionString="Data Source=..." providerName="..." />
</connectionStrings>
Swithcing between them then becomes a matter of changing the configSource on the <connectionStrings /> element.
Scott Hanselman has a good article describing using different configurations for different environments using the build setting in the compiler. I've used this with great success in some of my projects.
Have a look
I've create a custom configuration section in my web.config file for my 3.5 web application. The custom configuration contains special characters listed below.
<add Key="2" String="â€"/>
<add Key="3" String="148"/>
<add Key="4" String="!X"/>
<add Key="5" String="¡§"/>
<add Key="6" String="¡¦"/>
<add Key="7" String="¡¨"/>
<add Key="8" String="’" />
<add Key="9" String="–" />
I currently have the xml type of the web.config defined as below.
<?xml version="1.0"?>
This works fine in one development environment, but when I migrate the application to another environment I get an xml parsing error on the string for Key 2. When I replace all the string definitions for each config entry with standard alpha chars, the application works fine. Is there a way to enforce the XML to be read the same way? I have looked into the encoding attribute for the xml definition tag, just not positive what to set it to. Any guidance would be well appreciated. Thanks in advance.
I recommend setting the encoding to "utf-8". Make sure the file is also actually saved as utf-8.
Are you typing these into your web.config file manually? Can't you just HTML-encode your string values?