?
Solved

DLL loads Machine.config instead of my App.config

Posted on 2009-05-15
3
Medium Priority
?
1,067 Views
Last Modified: 2012-05-07
Hi All,

I'm having a strange issue I've never ran into before.  I have a VS2008 solution that contains a web service project, a dll project that contains core functions/types and a dll project that has all of my data access code in it.  My web service has a web.config file that works fine, can load properties no problem.  My data access project didn't have an app.config file so I added one using, Add->New Item->Application Configuration File which created an app.config file.  I put my connection string code in, like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=MyMachine\\SQLEXPRESS;Initial Catalog=MyDB;Integrated security=SSPI;" />
  </connectionStrings>
</configuration>

In my data access dll, I try and access the connection string like so:

string cnstring = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString.ToString();

This fails with a null reference error.  So, to try and find out what was going on, I used the following code to try and see what it was using:

ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
string test = ConfigurationManager.AppSettings.ToString();

if (settings != null)
{
     foreach (ConnectionStringSettings cs in settings)
     {
          Console.WriteLine(cs.Name);
          Console.WriteLine(cs.ProviderName);
          Console.WriteLine(cs.ConnectionString);
     }
}

The value for the name in the connection string is "LocalSQLServer" which tells me it isn't loading my App.config file for some reason.  I did some searching and found that it's possible to remove items from the App.Config file so I changed my App.config file to look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <clear/>
    <remove name="LocalSQLServer" />
    <add name="MyConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=MyMachine\\SQLEXPRESS;Initial Catalog=MyDB;Integrated security=SSPI;" />
  </connectionStrings>
</configuration>

I still get the same results, it only loads one connection string and it's name is "LocalSQLServer".

Has anyone seen this before or could lend some insight to why this is happening?

Thanks in advance,

Craig
0
Comment
Question by:ichikuma
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
b_levitt earned 2000 total points
ID: 24394442
Everything uses machine.config.  Other config files are just applied on top of it.

I think I understand what's going on.  Your expecting your dll to use a separate app.config.  But if you're using this dll in a web project, then the config file loaded for the process IS the web.config.   Which means you need to add your code from your app.config to your web.config.
0
 

Author Closing Comment

by:ichikuma
ID: 31581876
Thanks so much for the insight and for the explanation.  It makes total sense now and it works perfectly by adding it to the web.config.

Thanks so much for your help,

Craig
0
 
LVL 11

Expert Comment

by:b_levitt
ID: 24400713
no problem.  I'm glad one post is all you needed.  Good luck.

B
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

809 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