How to tell C# which config file to use? [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reading dll.config (not app.config!) from a plugin module.
I have two different projects, say A and B. I need to use some classes of A in B. So I added a reference to A in B. When I tried to run the application, I stated getting Object reference set to null exception. On investigation, I found that when I access classes of A from B, control goes to project A, but C# still uses config file of project B instead of using project A's config file. How do I get around this? How can I "include" A's config file in the dll?
I have gone through this blog but I feel it is a very dirty way of doing it. There ought to be an easier way!
Let me know if the question is unclear..

I believe .NET will always load the app.config file associated with the application rather than any libraries. There are complicated ways of specifying your own locations for config files - or just using your own configuration framework instead of the built-in one - but I don't think you can just ask .NET to load a config file per DLL.

Why don't you just all the necessary settings into the config file from project B? If you use some tools and libraries from an external vendor you just do the same stuff to configure it.
If you abstracted the configuration good enough you should be fine. Using another configuration file than the default one - well i would consider this as bad practice.

As I understand this you want integrate a app.config into your dll. Check this out:
How do you load the app.config file into a DLL

You can copy the relevant sections of A's config into the B config file and it'll work properly, but it's a bit tedious to say the least. I suppose you could automate it with a custom tool though.

Related

How to read the properties of an .resx file in a Web site project of asp. Net from a dll assembly

I'm new in .Net and working with two projects in c# a class library project(dll) and a website asp project.
I need to read some properties from a file .resx that is in the App_GlobalResources folder of the website.
Is there a way to read these properties in the .resx website file from a dll assembly more specifically in the method onPreRender??
Thanks for you attention
It sounds to me that you are having some problems with structuring and dependencies in your solution. (Trying to reference the website from a DLL)
Generally speaking, your DLL should not need to access the resources of the website on its own - you should only pass them in through as parameters when calling various methods that are contained in the DLL itself.
Have you thought about migrating the resource file to the DLL?
That would allow both DLL and the website to read from it.
Another option would be to migrate the setting you need to the .config file which you can read by using the ConfigurationManager class ( MSND Link )
You should be able to use it like this, even from your Code repository project:
string settingValue = ConfigurationManager.AppSettings["YouSettingNameHere"].ToString();
However, if you really want to keep your current solution structure you can follow the answer that Pavel Chuchuva gave on a similar question here.

C# Changing DLL app.config depending on consumer

I have a Class Library project containing some basic logic.
The DLL created by this project will be used in a few other projects.
I have a app.config file in the Class Library project with a couple of values the DLL uses.
When each consumer project will use the DLL, it has to change the values in the app.config
For example, if my DLL's app.config contains 3 settings: A, B, C, then:
The first consumer of the DLL will have A="a", B="aa", C="aaa" .
The second consumer of the DLL will have A="t", B="tt", C="ttt" .
and so on...
From design point of view, what is the most clean way to achieve this scenario?
(It seems to me that the app.config should reside at the project that uses the DLL)
Thanks for your attention! :)
EDIT:
Most of my code in the DLL is consuming ASMX web service which includes it's .config . Each application that will use the DLL, has it's own WS address (the contract is identical). How can I inject the address of the service from the application into the DLL?
EDIT #2:
Now I have 2 config files:
1. In the class library project - contains the WCF client config.
2. In the application that uses the DLL - contains the config with the values for the DLL.
How can I inject values from the application's config into the DLL's config (for example the address of the endpoint) ?
Only applications have a .config file, so having a .config file in your class library is useless.
That means that the values should come from somewhere else
The options I can think of are:
Use the .config file of the application - disadvantage: The person that write the application need to know about your config values and add them. Most of the time he/she will find out about these setting when they will get exceptions for missing values.
Save values to a DB (or some other web service) if all your applications use the same DB this can be a good idea. Works nicely when you have a very limited number of applications. I use this for different values for production/test environments
Make every class that needs these values to get them in a contractor. advantage: no hints needed, the application programer will be aware to data. This is a clean interface. I would use this option if it's a customer specific project. disadvantage: lots of work for the applications programer

Include DLL into Exe [duplicate]

This question already has answers here:
Can a .NET windows application be compressed into a single .exe?
(12 answers)
Closed 9 years ago.
I am new to C#.
I am not a good programmer but I have worked on some projects which were in Delphi7. In Delphi there is one option; compile the class files to create .dcu. Those .dcu can be used without having the actual code with any other project in Delphi and after build .dcu files need not no given to end user.
Now my actual Question,
Is there any way where I can create some compiled file from my C# class and use that compiled unit with some other project in C# ?
I already tried to create dll and use. Nice Idea, but I don't want to give any other file than Configuration File and Executable file. Since, I created dll that needs to give with application.
I also tried to create a setup of application which build executable, configuration file and dll together. while executing, it extract itself in %temp% folder and executes from there only. So, In this case I am not able to set the configuration file's values.
Again the same question,
Is there any way where I can create some compiled file from my C# class and use that compiled unit with some other project in C# ?
Please suggest the solution
Thanks
Make it into a .dll. And then add that .dll as a reference in whatever project you want by clicking on References and then selecting Add Reference, and then in the Browse tab navigate to your .dll and select add. You will now have access.
Basically you need to combine all your DLLs and the EXE to a single EXE?
You can use ILMerge.
For more options take a look this question: Can a .NET windows application be compressed into a single .exe?

Sharing EntityFramework Datamodel

I'm using EntityFramework 4 in my WPF desktop-application (NS: MyCompany.MyProduct).
Now I want to create the same application in ASP.NET (NS: MyCompany.MyProduct2), with the exact same functionality... Hence I need to use the exact same database as the WPF application already does.
Additionally, I want to create a new executable (hence a new wpf project) on top of my primary WPF project, that also uses the same ConnectionString like the WPF / ASP.NET-Application, to display some reports.
So I figured out I'd need to share the .edmx-Model (NS: MyCompany.MyProduct.Models.DBModel.edmx) and the ConnectionString that is already persistent in the app.config of the WPF app or the web.config of the ASP.NET-App.
What is the best or recommended way to do this?
What is the best or recommended way to do this?
Create a class library project and put EF model in there and share it between your WPF/Web projects. The app.config file of a library project isn't picked up by the parent project therefore you will have to manually update your web.config file to add the ConnectionString section.
This approach allows you to share business logic between your WPF app & your web app. If they are essentially the same app but on different platforms, then you should only be re-implementing the UI - this is one of the major advantages of the MVC pattern.
Agree with #James here. Don't be afraid of adding library projects to your solution. So you would have a project called MyCompany.Model that contains your EDMX. (Actually, you might find later that you want to use the T4 generation to split your model off from your DbContext or ObjectContext, but that's another discussion.)
With Visual Studio you can actually add a project--your EDMX project--to more than one solution. Be careful not to make changes to the EDMX project when editing one solution that break the other, though.
Respectfully, you may find that it's not ideal to use the GAC here, especially if your EDMX is still evolving.
As for connection strings, these are one thing that you tend not to share between projects. Typically they are in the app.config (or web.config) for your executable project. This can be a gotcha, because if you use a library project to hold your EDMX, EF will automatically create an app.config in the library project, with the connection string in it. But .NET never uses an app.config for a DLL. The only reason it's there is to give you something you can copy/paste into the real app.config for your executable (WPF) app.config or the web.config.
If your goal is to share the single .edmx dll between all three applications on one machine, the best way to accomplish this is to sign the dll, then add it to the GAC. If the dll will remain on different servers, there is no need to GAC the dll, you can just reference it in your projects, and add the connectionstring entry in the respective .configs.
GAC: http://msdn.microsoft.com/en-us/library/yf1d93sz(v=vs.100).aspx
Install a DLL to the GAC: http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx

Can someone provide a quick App.config/Web.config tutorial?

I've used these two configuration files many times before, but I've never taken the time to fully understand how they really work. As most people do, I understand the basics in how to call WebConfigurationManager.AppSettings["key"] to get config values.
Here are some questions I came up with:
What happens when you reference a configuration value within a class library, and the library is part of a bigger solution? Does the app.config need to be copied to the output directory in order for the variables to be found? (I assume yes)
Can you directly use a configuration value from an app.config in another class library?
Assuming question 3 is "yes", what happens if there are multiple app.config files from different libraries containing configuration values with the same key?
What happens when you reference the web.config, but in a class library?
What happens when you reference the app.config, but in a website or web application project?
The underlying answer to all of your questions is the same: Unless you set up something unusual, all assemblies in your project will read from the same configuration file. In a web app, they will all read from "web.config". In any other project type, they will read from the starting assembly's config file.
The app/ web.config that is used is the one that starts the process. Easier if I give an example:
Assume all projects in a solution have an app or web.config.
A test in project A calls code in project B that calls a web service in project C which calls code in project D.
In this case code in project A and B will use the app.config in project A. Code in project C and D will use the web.config in project C.

Categories