[Mono-list] Config file for dll

Helge Lenuweit helge at lenuweit.net
Tue Feb 7 08:06:21 EST 2006


Hello Peter,

I use that same code to read assembly settings and found it best to use 
the Assembly.Location property. I use the following code with both .NET 
and mono:

string cfgFile = asm.Location + ".config";
const string nodeName = "appSettings";

Note that I also changed the node name from assemblySettings to the 
standard appSettings. I know this is semantically incorrect as the DLL 
is not the "app", but it makes interchanging config files a lot easier 
and editing is less error-prone.

Best regards,
Helge



Bradley, Peter schrieb am 07.02.2006 12:06:
>
> 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 J
>
> 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
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>   


More information about the Mono-list mailing list