Solved

DLL loads Machine.config instead of my App.config

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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