I am setting up my application for CI&D. I created a DEV-Deploy web.config transform which contains the connection strings for the dev testing environment.
Here are the contents of the Web.DEV-Deploy.config connection string section:
<connectionStrings xdt:Transform="RemoveAttributes(configSource)">
<add name="DbContext"
providerName="MySql.Data.MySqlClient"
connectionString="CXN_STRING"
xdt:Transform="Insert" xdt:Locator="Match(name)"/>
<add name="elmah"
connectionString="CXN_STRING"
xdt:Transform="Insert" xdt:Locator="Match(name)"/>
</connectionStrings>
It should look like:
<connectionStrings>
<add name="DbContext" providerName="MySql.Data.MySqlClient"
connectionString="CXN_STRING"/>
<add name="elmah" connectionString="CXN_STRING"/>
</connectionStrings>
I'm building using the command line and I have tried the following commands, neither of which work:
msbuild web\web.csproj /T:Package /P:Configuration=DEV-Deploy /P:TransformConfigFiles=true
msbuild web\web.csproj /T:Package /P:Configuration=DEV-Deploy /t:TransformWebConfig
The deploy task looks like this:
web.deploy.cmd /Y /M:https://MACHINEIP:8172/msdeploy.axd -allowUntrusted /U:USERNAME /P:PASSWORD /A:Basic
The web.config looks like this upon deployment:
<connectionStrings configSource="connectionStrings.config"></connectionStrings>
I have tested to the best of my ability on my local machine and have not been able to duplicate the issue. What do I need to do to get the transform working correctly on the build?
Our CI&D team put the build/deploy scripts into source control and after looking them over, everything above was correct, the problem was the path for the build command was wrong while the command itself was correct.
Once that was updated the web.config transformed correctly.
Related
I have a webb aplication where I want to take advantage of the web.debug.config file so that when debugging I use a test database. This does not work for me. I have this in my web.debig.config..
<connectionStrings>
<add name="connStr"
connectionString="Data Source=LocalSqlserverName;Initial Catalog=testdb;Persist Security Info=True;User ID=Myusername;Password=mypassword"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
And in my web.config file this connectionstring point to another database server. But when I debug I can see that the connectionstring in web.config file is used instead of the one in web.debug.config. What am I doing wrong here?
As #esiprogrammer says it normally only transforms on Publish from Visual Studio.
However you can transform Web.config on build. Add this to your *.csproj file:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
<TransformXml
Source="Web.Base.config"
Transform="Web.$(Configuration).config"
Destination="Web.config" />
</Target>
Keep the origin configuration in Web.Base.config. It's enough to enable transformation and it works for any XML config file.
Source:
https://stackoverflow.com/a/35561167/3850405
I am using Visual Studio Team Services, previously Visual Studio Online, to continuously deploy my web app. I have tried setting up the web.config transformation, I believe it is working after I have changed the configurations setting to Release | Any CPU as instructed here Web config transforms not working with Visual Studio online, VS2013 and Azure .
The issue I believe I am having is more with the transform its self.
Right now I have the following in my web.config
<connectionStrings configSource="connections.config"/>
I want this so I can avoid checking in connections.config and still have my db connections setup locally. What I want to do is replace the above line of code with something like this.
<connectionStrings>
<add name="umbracoDB" connectionString="blah " providerName="System.Data.SqlClient" />
<add name="EFdb" connectionString="blah" providerName="System.Data.EntityClient" />
</connectionStrings>
I am using
<connectionStrings configSource="connections.config" xdt:Transform="Remove" xdt:Locator="Match(configSource)"/>
and it seems to be successfully removing the configSource connectionStrings element. But I am still confused on how to add back my replacement connectionStrings and add elements?
Devin
One thing that you can do is to store your DB connection strings as secret variables in Build/Release and then use the Tokenizer task from Marketplace
to replace the connection string token with actual string.
Tokenizer task has support for the environments in Release Management.
If you want to replace your connectionStrings element during deployment, try usign the Replace transform:
<connectionStrings xdt:Transform="Replace">
<add name="umbracoDB" connectionString="blah " providerName="System.Data.SqlClient" />
<add name="EFdb" connectionString="blah" providerName="System.Data.EntityClient" />
</connectionStrings>
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.
I have a problem with configuring an EDMX file that lives in an other assembly than by web project. My project looks somewhat like this:
Project 1
--> Database.edmx
--> App.Config
Project 2
--> Ton's of .cs and .aspx files.
--> Web.Config with the proper connection string.
Inside Visual Studio the updating of the .EDMX file inside Project 1 goes smoothly and while I had the .EDMX file inside project 2 the application ran as it supposed to.
Anyone has an idea on how to configure the .EDMX file inside Project 1 to point to the connectionstring of Web.Config? (or should I use Project1.dll.config to configure Project 1?)
You have to change the * in the connection string for the assembly name where the .edmx files are in:
<add name="Entities" connectionString="metadata=res://*/Models.EF.Model.csdl|res://*/Models.EF.Model.ssdl|res://*/Models.EF.Model.msl;provider=System.Data.SqlClient;provider connection ... ;" providerName="System.Data.EntityClient" />
for
<add name="Entities" connectionString="metadata=res://Project2/Models.EF.Model.csdl|res://Project2/Models.EF.Model.ssdl|res://Project2/Models.EF.Model.msl;provider=System.Data.SqlClient;provider connection ... ;" providerName="System.Data.EntityClient" />
As it turned out, there were 2 problems. One was solved replacing the * in the connection string.
The second problem was the one described here: http://blogs.teamb.com/craigstuntz/2010/08/13/38628/
It had to do with the path .csdl, .ssdl and .msl files had as resources inside the Project1 assembly
Anyway, things function properly now
Easier way is to take connection string from Web.Config and copy them into App.Config and point EDMX's connectionstring to same DB information. e.g
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="aspnetdbEntities" connectionString="metadata=res://*/Data.PSBData.csdl|res://*/Data.PSBData.ssdl|res://*/Data.PSBData.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\aspnetdb.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Also you need to check if the namespaces are correct if you have moved Database.edmx from Project 2 to Project 1, which you can check by opening Database.edmx and goto code behind.
I've this Class Library, as a result of a refactor action.
I added an App.config file and added something like this:
<configuration>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when I run the application, debugging learns me this is totally ignored. The immediate window tells me:
ConfigurationManager.ConnectionStrings[0]
{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Key: "LocalSqlServer"
Name: "LocalSqlServer"
Properties: {System.Configuration.ConfigurationPropertyCollection}
ProviderName: "System.Data.SqlClient"
I've checked generated config file in the bin directory and its contents are identical to the App.config.
I try to read the App.config using:
ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString
Nothing out of the ordinary I'd say, but what is going wrong?
A class library doesn't get its own config; for an app named Foo.exe you need your configuration to be in Foo.exe.config. The exception here is web apps, where web.config is the naming convention.
This connectionString should be in the corresponding app.config of the exe that you are running.
Is the file being copied to output? Secondly the connection string should be accessible from the AssemblyName.Settings.Properties class which is generated from your code.