This question already has answers here:
How to access a custom config from my class library application?
(3 answers)
Closed 8 years ago.
This questions has been asked quite a few times here, but I cannot seem to apply any of the answers to my case. So here we go again.
I have two projects, ProjectA and ProjectB. ProjectA is a class library and uses Entity Framework, which (by default) writes its model settings to the App.config file. ProjectB on the other hand, is an executable with its own Web.config file and references ProjectA in order to satisfy its data access needs. Of course, the problem is that at run-time, ProjectB's web.config is read since it's the "executing assembly", which means I need to include all the connection strings from ProjectA's app.config in ProjectB's web.config.
I really don't like the idea of manually copying these connection strings. I'm wondering if there's a way to tell ProjectB's web.config to replace its connectionStrings section with the one in app.config of ProjectA. So in theory, I'd start with something like this:
ProjectA (App.config):
<configuration>
<connectionStrings>
<add name="MyModelContext" connectionString="blahblah" />
</connectionStrings>
</configuration>
ProjectB (Web.config):
<configuration>
<connectionStrings>
</connectionStrings>
<configuration>
After building the project/at runtime:
ProjectB (Web.config):
<configuration>
<connectionStrings>
<add name="MyModelContext" connectionString="blahblah" />
</connectionStrings>
<configuration>
Is this feature supported?
You can replace just about any app.config/web.config section with an external file by using the configSource attribute.
In your web.config, where connectionStrings.config is your app.config connectionStrings section from ProjectA:
<connectionStrings configSource="connectionStrings.config" />
The configSource is a path relative to the web.config file, so you can point it at any file within your application, e.g. config\connectionStrings.config.
Related
I'm porting an app from .Net Framework to .Net Core and try to use ConfigurationManager from System.Configuration.ConfigurationManager package to get a connection string from Web.config. The problem is that it only contains default connection string from machine.config. I know that ASP.Net Core use appsettings.json with IConfiguration as a default way to manage configuration but as I understand System.Configuration.ConfigurationManager package was added to simplify the migration. So that's the right way to use ConfigurationManager to get the data from Web.config instead of global machine.config?
Web.config content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="My" connectionString="sample data" />
</connectionStrings>
</configuration>
Looks like the file must be named App.config, even if it's web app. After renaming Web.config to App.config it's read properly
I have two projects in a solution in Visual Studio 2010, Project1 and Project2.
Project1 is the parent project and has a config file called ConnectionStrings.config that contains connection strings used by both projects.
I have added the ConnectionStrings.config file from Project1 into Project2 as a linked file.
When I publish this to our webserver it works fine, Project2 can read data in from our database using the connection strings from the config file in Project1.
When I'm working on local and debug Project2 I get the following error which I assume means that it can't see the linked file:
Unable to open configSource file 'ConnectionStrings.config'.
(C:\Project2\web.config line 10)
Project1 ConnectionStrings.config
<?xml version="1.0"?>
<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=***;Initial Catalog=***;Integrated Security=False; Password=***;User ID=***"
providerName="System.Data.SqlClient" />
</connectionStrings>
Project2 web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings configSource="ConnectionStrings.config"/>
</configuration>
How can I get Project2 to read this config file from Project1?
Thanks
NO, I don't think you can do that. If you really want the same connection string to be shared across multiple project then consider having that connection string specified in machine.config rather.
My project has 2 different config sections(one technical & one functional) and some connection Strings.
I would like to have in a same configSource file, the technical config section & the connection strings & in an other one the functional section. I know how to do this in 3 separate files but not in 2. It would be logical to have technical configuration like server hostnames & connection string in the same file.
My configuration files should look like this:
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="MyService.Functional" type="Logger.ConfigHandler, Logger"/>
<section name="MyService.Technical" type="Logger.ConfigHandler, Logger"/>
</configSections>
<MyService.Functional configSource="Config\MyService.Functional.Config"/>
<MyService.Technical configSource="Config\MyService.Technical.Config"/>
<connectionStrings configSource="Config\MyService.Technical.Config">
</connectionStrings>
</configuration>
MyService.Technical.Config
<MyService.Technical.Config>
<MyResourceServer value="tcp://MyServer:9000"/>
</MyService.Technical.Config>
<connectionStrings>
<add name="MyEntities" [...] />
</connectionStrings>
However if I mix the section MyService.Technical & the connectionStrings in the same file, the ConfigurationManager can't load any section anymore.
Do you have any tip to do this ? Is it absolutely mandatory to have 3 separate files for this case ?
From my experience, it seems to load the contents of the file as the inner XML of the referring element, which would mean you need to have different files for each section being externally referenced.
I have more than one solution projects for an application with using one app.config for each solution.
Can i do a separate configuration file (other than app.config) for common setting like db name (connection string) ?
Because currently i put these setting in each and every app.config file.
Please try following.
Create one config called "common.config" like below which will be common to all solutions and let's say it's located # "E:/myconfig". Please keep all common setting in this config.
<appSettings>
<add key="connstring" value="conn string value" />
</appSettings>
Now link this common config in you specific solution config lets say web.config using file attribute
<configuration>
<appSettings file="E:\myconfig\Common.config">
<add key="key1" value="value" />
</appSettings>
</configuration>
Hope this will work for you.
I'm trying to make this simple call:
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString)
And here's my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>
</configuration>
But I'm getting an error: Object reference not set to an instance of an object.
It can't find the connection string. What am I doing wrong?
A common mistake is trying to read a connection string from an app.config from a referenced project instead of from the executable project (the web site or .exe project). You may need to copy the config settings containing the connection string to your main config file.
Double check the existence and contents of your configuration file in the build directory. That code should work fine if the config file is in place.
You could also pull put the connection string into a local variable so you can be sure the null reference exception is happening where you think it is.
You might need to specify the providerName:
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient" />