Config file in multi project solution - c#

I have a Solution where one project is the Data layer and the other project is the web front-end, which has the DL project as a reference. DL project have its own app.config file which stores connection strings used by Linq-to -Sql (.dbml) file. The web front end also has a web.Config file where it has its own settings.
How Do
I need to make the DL project to use the connection string from Web.config file of Web front end and not from the app.config file?
Basically, if I refer the Data Layer in any WebApp, it would pick the values from web.config and not from the app.config file in the DL project

Copy the relevant configuration from the DL .config file into the web.config file. It should work that way.

Related

Setting connection string after publishing on IIS Server

I have two projects, an asp.net MVC project called Home and a c# class library project called Home.Dao.
Home is a simple MVC Project with no connectionstring property in its web.config
Home.Dao is a c# class library project and it is responsible off all the interactions between the code and the database. It has an app.config and contains a connectionstring property
When publishing the Home project in IIS server, I can only find these files (web.config, Hom.Dao.dll, ... etc ) there is no app.config published. My problem is I cannot set the connectionstring after publishing. So I have two questions in mind :
How is it possible to set connectionstring from web.config so this
connectionstring can be used on Home.Dao
Is possible to publish also the app.config ?
To answer your questions:
It is not only possible, it is necessary to set the connection string from web.config
You could copy the app.config to the output directory, but that wouldn't be of much use to you.
To explain why:
Configuration files are only created for the top-level application. For web applications this is the web.config file which is copied to the output directory. For executables the app.config is renamed to -executable name-.exe.config. Configurations are only read from the top-level application config. All settings you need have to be there. So if you transfer your connection string to your web.config, you're good.

Copy settings from one Web.config to another except AppSettings and ConnectionStrings

Is it possible, through code, to copy settings from one Web.config file to another (except AppSettings and ConnectionStrings)?
I have a situation where a single MVC3 project has been deployed to multiple servers in different locations. There is an auto-updater on all of these that will pull in the latest version. Typically when this would run, it would only overwrite the application folders and NOT the Web.config file.
I just upgraded the project to MVC4. This changes basically everything in the Web.config file except the AppSettings and ConnectionStrings. All of the installations of this project would have slightly different values here.
How would I go about writing some code that will update the Web.config file, but preserve all of the AppSettings and ConnectionStrings?
.NET provides ways to get configuration from other config file. Refer this article:http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx
Basically, you can use "configSource" attribute to define which config file to refer. Note that the configuration file should be in same directory. if not, Refer here to solve the problem. .NET Config Files configSource outside the application directory folder

How to access Configuration file in Class library from webpage as well as webform

I have several projects some of which are web applications and some are windows applications.
I wanted to globalize my settings, something like Connection Strings because all the projects use the same connection string.
So I followed this example How to share custom application configuration settings across projects in .NET
my XML file is in the class library and that is referenced in each project. The windows project are working fine with the file path which is something like this in the client project:
<configuration>
<appSettings file="F:\Classes\ConnectionStringFile.xml">
</appSettings>
</configuration>
I also want to be able to use this xml file from a web project. however, the web project would not accept the path to this file as shown earlier.
What is the best approach or say, best solution to have this connection string file shared amongst web projects as well as windows projects. What do i pass in
something like <appSettings file ="~/classLibrary/ConnectionStringFile.xml" ???`
The solution we use is to store shared settings in a custom folder beneath the CommonApplicationData folder (e.g. C:\ProgramData on Windows7):
var commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var settingsFilePath = Path.Combine(commonAppData, "MyProgram\\Settings.xml");
file= accepts relative paths, and *.config files are excluded from ASP.NET and IIS static file rules, so it is safe to drop any *.config file into an ASP.NET site. Just put it next to the web.config file (root of the web app), mylibrary.config, and file="mylibrary.config".

Why is App.config read only from the main project?

I have two c# projects:
1. A DAL project which consists of a sql compact.e database and a .cs file that calls the stored procs after reading the connection string from the project's app.config file.
2.A client project- a simple .cs that uses the DAL project to access the DB and display the results in a simply GUI.
I've ran into some issues with the DAL, since ConfigurationManager.ConnectionString was empty all the time and then I discoverd that if I put the connection string inside the client's app.config then it can read it.
I have two questions:
1. Why does it work that way? Why a file the resides inside the DAL project can only read the client's project app.config file and not it's own?
2. It seems rather silly to add app.config to each client project that connects to the same DAL project, hence to the same DB. How can I have one app.config file mutual to all of my projects? Or how can I make sure that the DAL project's .cs file will read it's own app.config file?
Thanks ahead

How to access external .config files?

I have an asp.net mvc website which of course has a web.config file. I also have an external project which is a class library that uses a .config file for its own app settings. The problem is when I run my web application those external app settings values are not included in the appSettings.
How can I get the external class library projects appSettings values?
You can get the external app setting like that :
var config = ConfigurationManager.OpenExeConfiguration("some.config");
var someKeyValue = config.AppSettings.Settings["someKey"].Value;
You need to either:
1. add those settings to your web.config file.
2. point to the external settings, and use a post build event handler to copy the output into your web project.
<configuration>
<appSettings configSource="my.config" />
</configuration>
Standard convention is that you add the settings to your web.config file. assemblies that are only dlls do not load their own config files. This allows people who use them to specify the settings in their own application.

Categories