[Mono-list] Config file for dll

Bradley, Peter PBradley at uwic.ac.uk
Tue Feb 7 06:06:58 EST 2006


Hi,

 

I've tried to post on this before, but never managed to get below the
size limit.  So this is a change of tactic :-)

 

The problem is that I would like to keep the connection string for the
application's db connectivity in a configuration file instead of hard
coding it.  However, since I'm making the db connection from a dll
rather than an exe file I can't use a normal App.config file.  What I
would like to do is to use the AssemblySettings class that can be found
at:

 

http://www.bearcanyon.com/dotnet/#AssemblySettings

 

This allows the settings to be accessed like this:

 

// AssemblySettings usage:

//

// If you know the keys you're after, the following is probably

// the most convenient:

//

//      AssemblySettings settings = new AssemblySettings();

//      string someSetting1 = settings["someKey1"];

//      string someSetting2 = settings["someKey2"];

 

This works in .NET on Windows, but not in Mono on Linux.  When I deploy
the project to my Linux server and try to run the db connectivity code,
I get an error saying that the table cannot be read.  I'm certain this
is because the connection string is not being read from the config file
because if I hard code the connection string into the program instead of
calling AssemblySettings, everything works just fine.  In other words, I
think the config file is not found under Mono.

 

Looking at the AssemblySettings code, I think the problem is here:

 

            public static IDictionary GetConfig( Assembly asm )

            {

                  // Open and parse configuration file for specified

                  // assembly, returning collection to caller for future

                  // use outside of this class.

                  //

                  try

                  {

                        string cfgFile = asm.CodeBase + ".config";

                        const string nodeName = "assemblySettings";

 

                        XmlDocument doc = new XmlDocument();

                        doc.Load(new XmlTextReader(cfgFile));

            

                        XmlNodeList nodes =
doc.GetElementsByTagName(nodeName);

 

                        foreach( XmlNode node in nodes )

                        {

                              if( node.LocalName == nodeName )

                              {

                                    DictionarySectionHandler handler =
new DictionarySectionHandler();

                                    return
(IDictionary)handler.Create(null, null, node);

                              }

                        }

                  }

                  catch (Exception ex)

                  {

                        string s = ex.Message;

                  }

 

                  return(null);

            }

 

In Windows, the variable cfgFile is set to (for example)
MyAssembly.dll.config and then the XmlTextReader object reads the XML
file (the config file) in the folder of the dll's calling assembly,
which would be the folder of the executable assembly that links to the
dll, under normal circumstances (assuming the assembly is not in the
GAC).  I don't plan to put the assembly in the GAC.

 

Is it possible that Mono is looking in a different place?  Is there some
code I need to add to ensure that Mono will look in some specific place?

 

Many thanks

 

 

Peter

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20060207/356a5d9e/attachment.html


More information about the Mono-list mailing list