Where to find the location of IIS manager connection strings - c#

I was getting an error saying that one of my connection string in my application's web.config file was already defined.
I checked in the IIS settings and when I checked the connection string property it was there already with Entry Type : Inherited.
So I went up the chain and went all the way up to the root of the localhost and checked the connection strings there.
I found a bunch of connection strings there as well.. but they're also all with Entry Type Inherited..
I checked the web.config file inside the wwwroot folder but didn't find any connection strings defined in there..
Where could these connection strings be coming from?...

Configuration files in .NET are inherited in the following order:
systemroot\Microsoft .NET\Framework\versionNumber\CONFIG\Machine.config
systemroot\Microsoft .NET\Framework\versionNumber\CONFIG\Web.config (ASP.NET only)
(application directory)\Web.config
So the connection strings that show up as "inherited" are specified in either of the upper two files.
Reference: MSDN: ASP.NET Configuration File Hierarchy and Inheritance
If you don't want to alter the machine-wide configuration, you can <clear /> them from being inherited in your application's configuration as explained in What does <clear /> signify when specifying a connectionstring?:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="..." />
</connectionStrings>

Related

Connection string updated on server when publishing

i'm trying to get my connection string values updated on production server during publish. I'm trying the following with no results:
<connectionStrings>
add name="DataConnect" connectionString="Server=Pepe;Database=Oyeti;Integrated Security=SSPI;" xdt:Transform="Replace" />
and also added the xdt:Locator, but i don't think it's the correct way:
<connectionStrings>
add name="DataConnect" connectionString="Server=Pepe;Database=Oyeti;Integrated Security=SSPI;" xdt:Transform="Replace" xdt:Locator="Match(name)" />
What i'm not seeing there?
thanks!
You can add the xdt:Transform="SetAttributes" xdt:Locator="Match(name) attributes to the connectionString tag in your transformation file (Web.Release.config) to transform the values in your primary config file (Web.config). As an alternative, you can also specify connection strings in your publish profile.
Although the default transform file contains an example that shows how
to update a connection string, in most cases you do not need to set up
connection string transformations, because you can specify connection
strings in the publish profile. You'll do that in the deploy to IIS
and deploy to production tutorials.
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations

Find out which web.config(s) have been loaded

I'm encountering an error indicating that the web.config being loaded by this particular sub-project of my solution has a connectionstring that conflicts with an existing entry from an already-loaded web.config
(Exception message is: Additional information: The entry 'connStr' has already been added.)
Is there a way to easily find out the list of all web.configs loaded/being loaded, so that I can ascertain where to conflict is arising
There is only one web.config that will be loaded, however, it will inherit from your machine configuration. For IIS this is here:
%windir%\Microsoft.NET\Framework\framework_version\CONFIG\machine.config
And for IIS Express in one of these places:
%userprofile%\documents\iisexpress\config\applicationhost.config
%userprofile%\my documents\iisexpress\config\applicationhost.config
$(solutionDir)\.vs\config\applicationhost.config (only for Visual Studio 2015 and above)
So you can remove the duplicate from there, or add a remove entry in your web.config, for example:
<connectionStrings>
<remove name="MyConnection" />
<add name="MyConnection" connectionString="..." providerName="System.Data.SqlClient" />
</connectionStrings>
I've had this problem before, I added inheritInChildApplications="false" in my main web.config. This way I know that my sub web.config will not have any conflict.

ConnectionString for assemblies

I have a website with a connection string listed in its web.config. The connection string is altered by the Publish feature so that it can reference a development database until it is released, when it references a separate release database.
The website accesses the databases through some assemblies, though. They are Class Libraries so they can't be Published, at least as far as I can tell. I read that the web.config would override the app.config connectionstrings, but that doesn't seem to be happening.
Whenever I Publish the release site references the development database, unless I alter the assemblies app.config files to reference the release database.
I don't want to have to remember to do that every time. How do I handle this?
You've got two issues here:
1. How to remember to publish the correct settings each time you publish:
One way to deploy such settings is by using web.config transformations in Visual Studio. This is pretty easy to set up and means that you do not have to remember to update the settings each time you publish.
As well as debug and release environments, you can also create transforms for "UAT", "Staging", "Beta" or whatever other configs you might need.
You might find these articles useful: here, here and here.
For example, here is a transform for a Release environment:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<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)" />
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
Above, you will see that the transform for Release mode sets the attributes of the MyDB connection string (xdt:Transform="SetAttributes"), removes the debug attribute from the compilation section and replaces the customErrors section with a new version.
It's a simple, yet very powerful technique.
2. How to get your assemblies to pick up the settings in the web.config
If your libraries have been written in the usual way, they should be retrieving their connection strings by simply accessing the [Web]ConfigurationManager.ConnectionStrings property. Like #Bob Horn says, they should then pick up the settings from the host process's config file (in this case the web.config of your web app).
However, sometimes you might find that a library is getting its settings from a .Settings file in the project, in which case things get a little more complicated. You will need to copy the settings section of the app.config in to the web.config. (You can also do this using the transforms technology described above.)
If you have access to the source code of the other assemblies, find the part of the code that retrieves connection strings. If it's not accessing the ConfigurationManager class, then that might explain why it's not picking up the web.config file.

Duplicate Connection String Error

I have a website deployed in a server. One day it threw an error saying the connection string name is already added. I checked the web.config file and it has only one entry in that name. I removed the entry from the config. Now the website worked well and fetched data from database.
Note: When I changed the name of the config file it show error.
I think, the issue is – the connectionstring part is cached in memory. Is it so? How can we overcome this unwanted behavior?
Config Files in Source Code
Release Config
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
Debug Config
<system.web>
</system.web>
REFERENCES:
Issue with unwanted connection string appearing in my published web config
.NET 2.0 App.Config connection strings includes unwanted SQLExpress default
That is not the issue - when you change the web.config file, the IIS process gets reset, so there can't be any caching involved.
What is more probable is that there is either a parent or child directory with a web.config file that contains the same connection string name - this is causing the error.
You can solve that in several ways:
Ensure there is only one web.config in the correct scope with the connection string name
Use the remove element:
<connectionStrings>
<remove name="theConnectionString" />
<add name="theConnectionString" ... />
<connectionStrings>
Refer Encrypted config file does not apply “remove” tag in connectionStrings for a related question

Reading Hibernate Properties from Web.config

The C# project I'm working on uses nHibernate and the connection string is in the web.config as a property of a Hibernate element. I need to read the connection string in the installer to get a connection manually without using Hibernate. I know I can use configManager.connectionStrings, but as the connection string is already defined in the Hibernate portion of web.config I don't want to copy it again into the connectionStrings element. So how can I access this?
You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.
<hibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>
<connectionStrings>
<add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>
This makes it available in your ConfigurationManager, but only referenced once.

Categories

Resources