tags:

views:

671

answers:

4

I have a class library that is usually called from a .net console or web application. It integrates with various components, and relies on an app.config or web.config.

If I want to utilise the class library from script (i.e. IronPython), how can I get the script to utilise the config file? Ideally I want to be able to choose the config file when I run the script, or by convention (config file sitting alongside the script file).

I don't want to change the ipy.exe.config if possible as this wouldn't scale for multiple configurations without having multiple copies of IronPython?

Any alternatives?

A: 

Translating this blog post into Python, this should work:

import clr
import System.AppDomain
System.AppDomain.CurrentDomain.SetData(“APP_CONFIG_FILE”, r”c:\your\app.config”)
oefe
This didn't seem to make any difference
Andrew Rimmer
Did you make sure to call this before the library tries to load its settings? The easiest way to ensure this would be to do it *before* you clr.AddReference the library.
oefe
Yeah its at the top of the script, but doesn't make any difference.
Andrew Rimmer
didnt work for me either
Simon Hartcher
this is not working
a b
A: 

You can always include additional sections within config files. In your ipy.exe.config file you can add an include to import external config settings; say myApp.config.

In a batch/command file you can always copy over a specific .config set into myApp.config and therefore run with different config files on demand.

Have a peek at this blog on how to achieve this; http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx

Liam Westley
A: 

You can look at the System.Configuration.ConfigurationManager class. More specifically, OpenMappedExeConfiguration method will allow you to load any .config file of your choice. This will give you a Configuration object which exposes the standard AppSettins, ConnectionStrings, SectionGroups and Sections properties.

This approach requires you to pass the name of the config file to your script as a command line argument or to have a code logic to choose the .config file at run-time.

I know no Python, so I would refrain from attempts to post sample code. :-)

Franci Penov
I have looked into this previously, but the business layer is made up of a lot of different 3rd party modules that access the web.config independantly. So I really need to make sure the whole app is accessing the right data.
Andrew Rimmer
A: 

For a workaround what I did was fill the AppSettings collection for the ConfigurationManager static class "manually", so I created a PY Script and run an "import" of it on IronPython and the settings then will be available for the class library. However I couldn assing values to the ConnectionStrings collection :(

my script looks like this

import clr
clr.AddReferenceToFileAndPath(r'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll')
from System.Configuration import *
ConfigurationManager.AppSettings["settingA"] = "setting A value here"
ConfigurationManager.AppSettings["settingB"] = "setting B value here"

It would be nice though to know a way to "load" a custom .config file to the ConfigurationManager class.

Marko