?
Solved

C# - using .config files with DLLs used by a web app

Posted on 2005-03-17
6
Medium Priority
?
305 Views
Last Modified: 2013-11-18
Hey, I have a solution that includes multiple class library projects, and a web project.  

One of my class library projects contains database connections, so I wanted to use a .config file to define the user, database, etc.  But i'm not so sure how to do this with a DLL.  

I basically want something like a .ini file in VB6, so I don't have to include it in the project, and I can have a different file for each server, and the solution doesn't need to be rebuilt if the file is changed.

I tried adding a .config through the IDE (VS.NET 2003), but it doesn't work when I run a sample ASPX page that uses the DLL.  I also tried adding <myAppName>.config and <myAppName>.dll.config to the folder that contains the DLL.

thanks in advance.
0
Comment
Question by:Sketchy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Accepted Solution

by:
shankar_ms earned 136 total points
ID: 13570508
You cannot use .config file directly with a DLL. You have to use config file with an exe and pass the dynamic parameters to the dll.

The following phrase is from the msdn
"Projects that compile a DLL cannot use dynamic properties directly, however DLL properties can be set dynamically by the .exe referencing the .dll"

-Sh
0
 
LVL 4

Assisted Solution

by:imperial_p79
imperial_p79 earned 132 total points
ID: 13598617
This is one clean way of doing it.

Have a web.config directly under the solution. At the solution, right click and add new item and add a dev.config (similarly for prod.config etc). In your solution, this will appear in a separate folder called "Solution Items". Define all your db connection, users etc in this config file.

Now in the project which you think should need this config keys, go to the project properties (right click on the project, properties) and under "COmmon Properties" go to "Build events". Under pre-build Event command line enter the following command.

XCOPY /y/i $(SolutionDir)Config\*.config $(ProjectDir)

IF EXIST  $(ProjectDir)web.config DEL $(ProjectDir)web.config
rename $(ProjectDir)dev.config web.config

whenever the build on the project happens, the correct version of the web.config will be added to it. This way we dont have to have duplicate web.config in all the projects.

Let me know if this works.
0
 

Assisted Solution

by:peatre
peatre earned 132 total points
ID: 13612524
As said above, only the application can access .config variables. Though just because of this does not mean you can use them. You could get the application to load the variable in and then either expose it or pass it as an arg to the called dll.

Example .config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="DBSource" value="ukdev304" />
        <add key="DBUser" value="new_stax" />
        <add key="DBPassword" value="" />
    </appSettings>
</configuration>

Then use the following code to access the variables in the .config file, make sure you have 'using System.Configuration;' in your using section of your .cs file.

// Read in the settings found in the application .config file.
private void GetConfigurationSettings()
{
    m_DBSource = Convert.ToString
        (ConfigurationSettings.AppSettings["DBSource"]);
    m_DBUser = Convert.ToString
        (ConfigurationSettings.AppSettings["DBUser"]);
    m_DBPassword = Convert.ToString
        (ConfigurationSettings.AppSettings["DBPassword"]);
}

Now that you have read the values in you can expose or pass them within your app in whichever way you wish.

Hope this helps!
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question