Share config file with multiple project in same solution - c#

My project use Entity Framework 6 Database First for the DAL, and it need to have a connectionString in every project's config file.
As I have multi project in solution, I found that it can be a problem if I need to modify connection string, which I need to check every config file.
So I come up an idea, create a Share config file under the solution directory.
The structure:
+ Solution
- CommonConfig
- Share.Debug.config
- Share.Release.config
- Project1
- web.config
- web.Debug.config
- web.Release.config
- Share.Debug.config (Add with Link)
- Share.Release.config (Add with Link)
- Project2
...
And I have move the connectionString section to Share.xxx.config
<connectionStrings>
<add name="db" connectionString="..." />
</connectionStrings>
First test with configSource, it work without problem
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<connectionStrings configSource="bin\Share.Debug.config"/>
...
</configuration>
Then I have remove whole section for connectionString
And modify the web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings configSource="bin\Share.Debug.config" xdt:Transform="InsertIfMissing" />
<system.web>
</system.web>
</configuration>
But it doesn't work, the EF cannot find the connectionString with this setting.
Will it a problem if I want to add a whole section instead of a node?
EDIT
After read several post, some said the transform only work when I publish the website, so I try to publish it to my local IIS.
When I create and preview the publish profile, it return error
A section using 'configSource' may contain no other attributes or elements.

Related

How to specify connection string for Entity Framework in WPF project?

I normally work with ASP.NET MVC web projects so the connection string always goes into the web.config file.
Where do I put it in my WPF project?
Put it in your app.config under the configSections tag
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
</configSections>
<connectionStrings>
<add name="MyModel" connectionString="MyConnectionString" />
</connectionStrings>
...
If you don't have an app.config file you can create one manually.
You'll probably have to create an App.config file in your project by hand. Then you can fill it up as usual.

How to set up Web.Config transformation to cater for Local and production configurations?

I have this dilemma of having to have different connection strings for the web.config on my local machine and to have a release transformation that would make the production binaries use the machine.config on the web server.
So I have these files in my Visual Studio solution:
Web.Config
Web.Debug.Config
Web.Release.Config
In the web.config I have removed and added new connection strings.
<remove name="connstring">
<add name="connstring" ConnectionString="blahblah" />
What I want to do is to have nothing in final web.config when deployed (by TFS build) to the web server so that my web application would use anything in the machine.config on the server.
How can I do that?
You can remove configuration settings with the RemoveAll transform attribute. Assuming you are deploying the Release build configuration, you can generate a completely empty web.config by putting the following in Web.Release.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="RemoveAll" />
This resulting web.config will have the XML declaration at the top and nothing else.
If you only want to remove certain configuration sub-sections, add the RemoveAll transform attribute to the section you want removed. For example, the following Web.Release.Config will remove all application settings:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="RemoveAll" />
</configuration>
See the full Transformation Syntax Documentation for more details.

How to deploy ASP.NET application to production machine without having to change DB connection string manually

As it stands, right now when I deploy my web application I always go into my web.config file to change the server name etc from the connection string manually before deploying the application. Is there an easier way to deploy a web app without having to always change the server in the connection string?
Thanks
Use a transform Web.config When Deploying a Web Application Project:
How to: Transform Web.config When Deploying a Web Application Project
Web.config Transformation Syntax for Web Project Deployment Using Visual Studio
It depends on how you are deploying your web app, but one common way to do it is to use web.config transforms
http://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx
we use WIX installer exactly for this purpose.
It can be customized, like in my case, to select DEV, QA or PROD env while installation.
Best thing is it uses underlying MSI installation framework, if thats the right word here.
Assuming you only need to change the connection string for just one specific Web Deploy then you can do that with a transformation as others have said. The following should show exactly what you need to do.
In Solution Explorer expand the Properties node to get the PublishProperties as in the following.
Right-click on the Web Deploy profile and select Add Config Transform as shown in the following.
You will get a Web.project - Web Deploy.config file in the Web Config node. The initial contents will be the following.
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
Change the sample to the following or add the following.
<connectionStrings>
<add name="csname"
connectionString="yourotherconnectionstring"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Where csname is the name in the Web.config of your connection string that you need to replace in the deployment.
There are many other transformations possible but if you only need to change the connection string for a specific Web Deploy then that should be the most straight forward. It is saying that you want to search for the connection string that has the specified name and then change the connection string value during the deployment.

AppSettings in App or Web Config Using a Linked File

I'm trying to reference some common config settings between a Windows Service and an ASP.NET MVC website. I am doing this by using the file attribute on appSettings in either the App.config or Web.config (respectively). The file (named common.config) that is being referenced is a linked file in a separate project in the same solution. That common.config is set to Content with Copy Always in both projects.
This stack answer to a similiar question seems to suggest at least for configSource this solution would work. I don't want configSource though as I only want a handful of the properties to be common amongst the two projects. Update: I just tried this, and the configSource also doesn't work. It can't find the config file. This leads me to believe the common.config is not treated as content with copy always.
Example App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey" value="1"/>
</appSettings>
</configuration>
Example Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey2" value="2" />
</appSettings>
</configuration>
Example common.config (Content -> Copy Always)
<appSettings>
<add key="CommonKey" value="1" />
</appSettings>
I am using ConfigurationManager / WebConfigurationManager reading from the AppSettings property.
Any ideas why when the common.config is a linked file, it's AppSettings values are not used and when it is not linked it works as normal?
Thanks!
In the Web.Config you must add "bin/" (se example below).
By default the web.config is NOT copied into the bin folder but the file common.config is, therefore you must add the path from web.config. In a non-web project the default behavior is that the App.config is copied to the bin folder with name MyProgram.exe.config and is in the same directory as common.config.
<appSettings file="bin/common.config">
The idea of using "bin/..." is good but leads to an error saying that "/" is an invalid character in the resulting virtual path.
The proper solution is tu use "bin...".
Cheers
I use this to access another .exe's config file, not sure whether it will work with a MVC project, but this might get you closer:
string proj2Exe = #"C:\projects\proj2\bin\Debug\proj2.exe";
Configuration proj2Config = ConfigurationManager.OpenExeConfiguration(proj2Exe);
string mysetting = proj2Config .AppSettings.Settings["ThatSetting"].Value;

Can I store settings in a settings.config file in ASP.NET MVC?

I'm using .NET MVC
I have about 10 properties I want to store in a configuration file (.config etc.), related to environment/deployment stuff, + other things for quick changes without doing dLL deploys.
I'm using Team foundation service for CI builds etc, and my web.config is obviously under version-contrl.
What I'd like to do is have a settings.config (that's not in version control) file to store these, am I able to do this?
Or does it need to be in web.config?
To answer the title question, yes you can store settings in a separate config file, to do so you need to define the configSource property of appSettings element
E.g.
<appSettings configSource="settings.config" />
and in the settings.config file
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="settingKey" value="environmentValue" />
</appSettings>
However, for the sake of environment specific settings, you may want to look at config transforms. Setting up a transform config for each environment then deploying to that environment with the specified build configuration.
E.g. Web.Dev.config (provided you have setup a 'Dev' build configuration)
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="settingKey"
value="devEnvironmentValue"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
More details of build configuration and config transforms here: http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
Or you could take advantage of TFS features and parameterize the environment variables, I don't have a lot of experience with this, but the following should help: http://ig.obsglobal.com/2013/02/tfs-and-continuous-deployment-part-4-parameterized-deployments/

Categories