I am working on an asp .net project in which I wrote a custom library for the logging of data. I am planning to refer this class library in my project and I need to pass some details like filename, filepath etc and from the web project to the class library.
Since this information will change usually, I am planning to give this data in web.config file in the web project. I can read this configuration file data and pass the info to the class library.
Is it possible to pass the whole configuration object to class library and read from there?
In web config add in "configuration" section:
<appSettings>
<add key="logpath" value="c:\logs\log.txt" />
</appSettings>
And in code:
ConfigurationManager.AppSettings["logpath"]
Related
I gone through lot of threads about this topic but did not get any solution.
First I created a windows application. In that I used app.config file to store some variables it worked properly. Then the application converted to class library. While extracting the varibles it gives only null values.
Any other option to get or set variables in config file in the class library
Thanks.
If you converted you code to some class library you have to remember that it will reuse web.config/app.config of an application, which references it. So I would suggest to add settings from "old" app.config to the configuration file of an application, which uses your library.
We can not use app.config or web.config in class library. Add app.config for Console/Windows application or web.config file for your web application which calls your class library then application will automatically takes and uses that configuration file.
My problem is solved but I don't quite understand the solution and why it worked. I made a class library "Business Layer" inside a solution which also contains a Windows forms project (C#). Within the class library I put an app.config file and defined a connection string inside it, which I used in SQL-connection code in the business layer. Then I called the database logic in the Windows forms application, but the project failed at the connection string part (i.e., it returned null). However, when I copied app.config to the application project it started working. Why? Why is it that if I am using all database code in the business layer that pasting the code in the application project worked?
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=HOME-PC;Initial Catalog=LMS;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
getting:
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
You are using the following code in your program:
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"]
.ConnectionString.ToString();
So, what does this do? Why is this important?
ConfigurationManager will look for the app.config or web.config (if you are making a web site) that corresponds to the running program. For example, if you have a program called "MyCoolProgram", you would have two output files called "MyCoolProgram.exe" and "MyCoolProgram.exe.config".
It's because of this naming structure that the ConfigurationManager is able to work.
But you already made an app.config, right? You made one in your referencing dll project. Why do you have to make another app.config with the same values?
So let's add in another project to our example. So far our example solution looks like this:
MyCoolProject (exe)
CommonLogic (dll)
If both projects have their own app.config file, the files listed in MyCoolProjects' bin folder would look like this:
MyCoolProject.exe
MyCoolProject.exe.config
CommonLogic.dll
So first, where is the config file for CommonLogic? It's not there. By default, a class library's config file is not copied over with the dll file, but you can see it still exists in CommonLogic's bin directory.
Second, so let's say that you do copy over CommonLogic's config file. You will have a file called CommonLogic.dll.config in MyCoolProject's bin directory. However, with "MyCoolProject.exe.config" empty, your code still can't find the connection string. It's strange because,
The connection string is set (only) in CommonLogic.dll.config
The code that is trying to get the connection string is from CommonLogic
So what gives?
Remember what I said before: ConfigurationManager will look for the app.config that corresponds to the running program, not the assembly that the code was ran from. No matter where your code is, the Configuration Manager is going to look at "MyCoolProject.exe.config" for any values. It's not going to look at anything else. For all intents and purposes, it doesn't even know about any other libraries or config files.
This means that you need to put your connection string (preferably exclusively) in the program that will be consuming the referencing library.
The class library should not be in charge of setting the connection string, that should be up to the program that uses it.
when I copied app.config in actual application project too then it started working, why ?
Because app.config is for the application, not the library. VS just gives you an app.config for a library to show you what should be put in the application configuration file.
It's perfectly reasonable to assume that a library can be used in multiple applications with different configurations, so tying the configuration to the library would make it harder to separate the configuration between applications.
I'm creating a web application, which calls a DLL to run unit tests, I also have another DLL(DataAccessLayer) which performs connections and performs queries to SQL which references the main DLL. Both the DLLs use the same config file to read settings.
When running application from VS, the application is working fine. However when the web app is deployed to IIS, it seems the DLLs are unable to read the settings from the config file.
After some research I found that I might have to explicitly define the configuration elements in the web.config file, however I don't know how to implement this. Can someone please point me in the right direction?
I'm actually retrieving the settings using the ConfigurationManager with the following code:-
public string GetValue(string key)
{
var appConfig = ConfigurationManager.OpenExeConfiguration("path to dll");
strKeyValue = appConfig.AppSettings.Settings[key].Value;
return strKeyValue;
}
Thanks.
Use WebConfigurationManager.AppSettings["HelloWorldKey"]; to read AppSettings from the web.config.
Just set all the appSettings values used by the DLL you mention, directly in the web.config PRIOR to deploying the app. You don't need to modify this at run-time (and you shouldn't anyway, since any modification to the web.config will cause the application to restart)
Add the connectionstring or AppSetting or ApplicationSettings used in you app.config into your web.config, I understand this is a manual task but is the only way that the config will read the settings.
Use following code to access connection string
string filePath= WebConfigurationManager.AppSettings["Pathfile"].ToString();
Web config Fie
<configuration>
....
<appSettings>
<add key="Pathfile" value="Path to dll"/>
</appSettings>
....
</configuration>
I have a VC2010 C# Solution, with a number of projects in it.
So for example, I have a web project, and I have a class library.
In the web.config file, I have a key in the <appSettings> section, e.g.
<add key="FileDirectory" value="G:\ftproot\sales" />
I have also added a key in the Web.Production.config file to reflect the file directory on the server.
So when I reference it in my web project (It's MVC) - I do so like this:
var FTPPath = ConfigurationManager.AppSettings["FileDirectory"];
this works fine within my web project. However, I also need to reference this in the class library, which gets to my question - Is there a way to reference a key in the web.config file from another project, e.g. a class library, in the same solution??
All help is appreciated.
Thanks
Yes you can use exactly the same code. .Net will look up the configuration key in the config file of the application which started the app domain. A class library used by such an application will have access to it's config file.
class libraries do not have their own configuration. They use the configuration of which ever executable they are being used in.
This means that for you you should be able to use the same code, and it will read the setting from the config (assuming that it is there).
This is not always convenient though (for example if you write a .net based plugin for a MMC snap-in, as this means you have to modify the mmc.exe.config in the system folder.)
You might be better having a method to pass this required configuration setting into you library code. then in apps where you control the config you can just read it from there and pass it in, and in apps where you can't you can use another approach, like reading from the registry or from a manually read config file. Or have the best of both worlds and make it so you can pass it in, and if this is not done it attempts to read it from the default configuration.
This question has some more details on the pitfalls associated with dll configuration, but also has some techniques for doing it if you need to.
I have a web project (mvc) and data access layer in a separated class library project. I need to access to a connection string in app.config which sits in that library project.
ConfigurationManager.ConnectionStrings[0].ConnectionString pulls something strange. I don't have this kind of settings neither in the library's config nor in the web project's config files.
the App.config looks like that:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DALConnectionString" connectionString="User ID=sa;Password=pass;Initial Catalog=db;Data Source=srv\SQL2005;" />
</connectionStrings>
</configuration>
Your library should use dependency injection in this case for inversion of control.
Your class in the data access layer (DAL) library should take the connection string as a constructor argument or a property value.
This will make sure that your DAL can be used in other projects also and is not tied to your your mvc web application.
Let the code which will consume the DAL read the connection string from the config file and inject it into your class's constructor.
By default, a class library can't access a config file.
The client of the class library, in this case your web project, can provide config settings.
Therefore, put all the relevant settings, the connection strings, in the web's config file. The ConfigurationManager code in the class library will use the web projects config settings.
you should add the fragment shown above in the web.config then at runtime the configuration manager will use it even if running inside your class library.
You cannot access a app.config for a DLL.
app.config only works for the entry point assembly or web.config for a web project.
Try copying the connection to the entry point config or load the config by parsing the configuration XML - not recommended.