[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

DLL loads Machine.config instead of my App.config

Posted on 2009-05-15
3
Medium Priority
?
1,060 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
[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
  • 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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