When creating an ASP.NET MVC project, a web.config file gets generated automatically and is used for all the connection strings and dll versions relevant to the solution.
I've added a new project to the solution for handling data operations using Entity Framework and can see that it has it's own app.config that gets created as well.
Previously, all data and web was on the one ASP.NET project and now I've split them up into two different projects so I can have the web project just have web components and data project have database connections using Entity Framework.
Initially it looked like the app.config of the data project would require the connection strings to the database be present as well, but it looks like the data project can connect fine to the database using the connection strings from the web.config file.
I've tried removing the app.config file completely from the data project and it looks like the solution runs even without it.
Are there any special reasons the app.config file exists in an ASP.NET MVC project or can this file be safely deleted without affecting the website?
For example, I tried putting in 2 different connection strings, one in web.config and a different one in app.config. When accessing the site, it looks like only the connection strings and app variables from the web.config file is used.
Is there some sort of hierarchy that gets used where the app.config details will override the web.config details?
In .Net, there only have three types of project: windows application, console application and class library.
The app.config will be generated automatically with the first two type applications, and it will be used while the application running;
For the web site/application/services, the web.config will be generated and used while accessing web pages. But actually, the web site/application/services are class library type.
In your case, the Data project might be created as a console/windows application, so the app.config will be generated. So you should change the Data project to class library and add a project reference in the Web project, then you can delete app.config safely.
To change project type:
Right click the Data project -> Property -> Application in the left panel -> Output type -> Class Library
To add project reference:
Right click the Web project -> Add -> Reference.. -> Projects in the left panel -> check the Data project -> OK
Finial, the reason why only the connection string in web.config is being used is: the Data project is treated as a class library and it will fetch the configurations from web.config at the runtime. And if you run the Data project alone, it will fetch the configurations in app.config instead.
Both are used to configure the project, the only difference is the type of project.
From the source
Web.Config is used for asp.net web projects / web services. (or Say Web application)
App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications (or say window application)
So might be your second project for data is window or console-based, that's why created.
Until any file in a project, you do not have the reference in other files, you can remove and run the project.
Ideally connection string at your main project and give the reference in the data project which is handling the single connection string in the solution or in multiple projects.
https://www.c-sharpcorner.com/UploadFile/8a67c0/web-config-vs-app-config-vs-machine-config/
Related
I'm trying to use application settings with a C#.NET project I am working on. But I can't seem to get it to return anything other then the default values. Through the project properties I've added a single setting, DBConnectionString, and set its value to a connection string I want to use. Its scope is set to "application".
Doing this created a number of files including Settings.settings, Settings.Designer.CS, and app.config. The Settings class then has custom, type safe, properties that can be used to set and retrieve each setting. The app.config file is a XML file that stores the actual settings values.
When I build my project it looks like the app.config file is copied to the target directory as DataAccessLayer.dll.config. My program runs fine and is able to use the default connection string.
Next I tried to edit the DataAccessLayer.dll.config file to change the connection string. I ran my program again, but it continued to use the default value.
This project is a class library that use to a Web project and sometimes the connection string can changes.
Builds will output config files named after the dll however those aren't actually what's read on app start up. You could put the setting in the web apps config (example here Equivalent to 'app.config' for a library (DLL)), those are the settings you'll actually be running with in this case.
If you want your library to be portable you'll have to either; 1) make your own config class/file 2) Read your dll's app config manually (example in the answer I linked to above) or 3) Put your setting in the importing projects app.config
In this case I would just put your connection string data in the web apps config. If DataAccessLayer.dll is only for internal use, this is in my experience the most common pattern, and doesn't really have many cons. Ultimately I would have these values set during my build or by a deploy utility like Salt or Chef. If you're manually editing the web apps config on or after deploy then you're doing it wrong.
I have a class library with a method that takes a string and returns an xml document. This library references another project - containing edmx models generated with EF6 db first.
I realize that my connection strings from the EF project must be moved into the "executing" application config file - this is fine in development as I have created a simple web client as my startup project and it's web.config contains the necessary connection strings.
When I deploy this solution to my QA server, the "executing" application is actually a VB6 app and I am getting this error. I have no idea what application config file is being used in this case.
Any suggestions?
I have a Visual Studio web service application with the following solution structure (using VS2013 Community):
- [Solution] S
- [Project] S_Service
- S.amsx
- [Project] S_Lib
- File1.cs
- File2.cs
- app.config
The S_Service project is a simple web service project, with just a single asmx file with one WebService method. The project contains a reference to the S_Lib project, a class library to do all the work in terms of the business logic (the request processing).
In S_Lib I have an app.config file in which I store things like directories and file names for stuff which is used by the various components in S_Lib. When I am developing, changes to that file are picked up by the code ok.
Here's the problem: When I publish the S_Service project, the publish directory doesn't contain my app.config - only S_Service.dll and S_Lib.dll. After reading some other posts on StackOverflow (can't seem to find them now), I tried setting the build action on app.config to Content and to Copy Always. Great, this gets the file across to the publish directory, so it looks ok. But, once I deploy the whole lot onto IIS, any changes to the app.config file do not get reflected when the service is run. In fact I can delete the file completely from the IIS directory and it runs just fine. It's as though S_Lib.dll contains a compiled version of the configuration settings. This is no use, as I want to modify the config depending on the machine it's deployed on.
What do I need to do so that app.config is actually used at runtime and that changes are read on the fly?
Just as you wrote, S_Lib.dll contains compiles settings from the time when you set them in VS settings designer. Therefore it is still working (more or less).
You have a web service so you need a web.config. Add one to S_Service project. Then merge app.config content to web.config. Every time you change some setting in S_Lib project you will have to merge changes to web.config as well.
Or you could add app.config to S_Service project as a link by name web.config (not sure if it is possible to create a link with different name). Then when you change settings in S_Lib project they will be referenced in S_Service project automatically.
After failing to find a simple Visual Studio-based solution to do what I want, I implemented a more customised solution. In the library project, I replaced the config lookup method:
internal static string GetConfig(string key) {
return ConfigurationManager.AppSettings[key] as string;
}
with a new method that reads my own settings file (custom format), stored in the solution. It's not perfectly ideal as it means that each project in the solution has to have its own settings file, but it's simpler overall. If anyone is interested please leave a comment and I will elaborate on this solution.
I have a solution with the following setup:
X amount of class library projects
Y amount of console application projects
Each of these projects may have 0 or more configuration parameters.
Now, I'd like to have only one App.config for user to specify settings and that App.config will only contain parameters of all the reference projects of the console application project to be run.
I've tried giving each project a Settings file and then linking them to the console applications according to their dependencies but that didn't work.
I've also tried just lumping all the configurations together in one class library project and have each console application link to that app.config (or settings file). But that also didn't work (i.e. changes of the app.config or the settings file in the class library will not update the .config of the executable)
Is what I am trying to do possible?
Yes, it is possible. You just need to open the app file. Follow the next example:
ConfigurationManager.OpenExeConfiguration("C:\Test\SomeProject.dll");
XmlNode loggingConfigNode = ConfigurationManager.GetSection("log4net") as XmlNode;
I guess that you will have to open each setting file in order to use the settings, or you will have to consolidate all the settings in a single app.config and then your applications can access the file by open it.
Let's say I have the following scenario in .NET 4.0:
- Solution containing: a) a Class Library b) a Console Application
The Console Application references the Class Library.
What I want is to setup some Application Settings in my Class Library and make it so that these are accessible by the Class Library (host Console Application should not make use of these directly), but also modifiable via a .config file after deployment (so if the user decides they want to change the value of one of the settings, they can do so without having to re-deploy the application.
Is this possible?
As far as I know, the class library will have access to the host files config file via dot net configuration management framework. You can also have a second configuration file with a name known to your library and load it manually at library initialization using the swme framework.
Please Reference System.Configuration
var configurationFileMap = new ConfigurationFileMap(#"c:\myconfig.config");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(configurationFileMap);
Both the host and the lib should be able to access the myfile.config as long as they know the name of it. If you want to make it more general, you can store the name of the config file under a known registry key
Figured this out after quite a bit of Google-fu.
The final solution I adopted was the following:
I created Settings (which generated an app.config file) in my Service application. I then created links to these settings in both my Code Library project and Console Application project (this is done by going to Add > Existing Item > then hitting the drop-down arrow next to the Add button and selecting "Add as Link". Although the one in the Code Library project is not necessary.
What this does is make it so that I only have 1 settings configuration while in development, while it will still generate a config file for my console application which I can access from both the console application and the Code Library during the development process.
Finally, I used the code below to open the configuration file and access the values. As a disclaimer, there may be an easier way of doing this, but I tried about a 100 combinations and nothing else worked:
String cfgFileName = "IntercompanyConsoleApp.exe.config";
ExeConfigurationFileMap cfgMap = new ExeConfigurationFileMap();
cfgMap.ExeConfigFilename = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + cfgFileName;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(cfgMap, ConfigurationUserLevel.None);
// IntercompanyService is the name of my service app, which is where the real app.config file resides -- hence the entries in the xml are based on this application.
// Also, the scope of my settings entries is Application
ClientSettingsSection section = (ClientSettingsSection)cfg.GetSection("applicationSettings/IntercompanyService.Properties.Settings");
Console.WriteLine(section.Settings.Get("Server").Value.ValueXml.InnerText);
Console.WriteLine(section.Settings.Get("Database").Value.ValueXml.InnerText);
This is an obscure issue, but hopefully this saves someone some time in the future because I spent about 4 hours trying to figure this out.