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.
Related
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>
I am trying to publish a website to make it go live and need to connect to a database. Currently I am using:
<connectionStrings>
<add name="UniversityContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=RoomAudit;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\RoomAuditSystem.mdf" providerName="System.Data.SqlClient" />-->
</connectionStrings>
in my web.config which obviously using my local machine.
I've looked online and on here and can't seem to find a clear cut tutorial. Here is the database details:
DATABASE SERVER
mysql4.gear.host
with my username and password with reading and writing permissions.
If someone could point me in the right direction I would appreciate it - I'm also using Entity Framework if that makes any difference?
EDIT:
This is my new connection string:
<add name="UniversityContext" connectionString="Server=mysql4.gear.host;Database=databasename;User Id=username; Password=password;" providerName="System.Data.SqlClient" />
However, when I publish the website I get a runtime error?
Runtime error:
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
Console error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
IMO, best place for learning to write connection strings is here: https://www.connectionstrings.com/sql-server/
Try this
<add name="UniversityContext" connectionString="Data Source=mysql4.gear.host;Initial Catalog=databasename;User ID=username;Password=password" />
I suggest to create file with .udl extension and test configuration.
It could help you to find proper parameters.
Can someone please give me the technical reason why <clear></clear> is invalid in app.config vs <clear />
It's typically used like this :
<connectionStrings>
<clear/>
<add etc..... />
</connectionStrings>
Problem is I'm using an installer product (InstallShield) with does xml transformation to app.config files, and it's changing <clear /> to <clear></clear>
This breaks the application (service won't even start)
For now I'm just looking for the technical explanation, but if you have any workaround that would be nice too.
It seems that using <clear></clear> breaks the parsing of the connectionStrings section (and probably others too, if used there). See the comment of Hans Passant for a possible explanation.
If <clear></clear> is present, the ConfigurationManager.ConnectionStrings is empty. So the reason for you service crashing may be, that you don't check if this collection contains any elements or try to access them via ConfigurationManager.ConnectionStrings["nameoftheconnection"] which throws an exception if there is no element with that key.
Possible workarounds (which I admin are both rather clumsy and hacky)
You remove the <clear />. This may lead to some additional connect strings created by some referenced assemblies. But as your own connectionstring should be at the end of this collection, you may be able to remove the unwanted entries.
You add a custom task to your installation routine, which processes the app.config file in one of the later steps and replaces <clear></clear> with <clear />
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.
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