Solved

DLL loads Machine.config instead of my App.config

Posted on 2009-05-15
3
1,031 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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now