app.config in IronPython without additional assemblies
29 October 2012 - Iron Python
Problem and Motivation
One of the most important features of IronPython is its interoperability with existing .net assemblies. They can be easily referenced, imported and used like in the following sample:
In this sample I add a reference to a CLR-library called SomeLibrary, import one of its classes called Helper, call a static member named GetSomeValueUsingConfig and print its result using python. The implementation of the Helper class is very simple and just creates some string values which use the ConfigurationManager to access the application configuration file and the contained AppSettings.
The App.config contains the following xml data:
During building or deploying of a .net application the App.config file is typically copied to the location of the resulting library (e.g. SomeLibrary.dll) or executable (e.g. SomeApp.exe) and has to conform to a strict naming convention to be discovered by the runtime (e.g. SomeApp.exe.config). In case of a IronPython script the .net runtime is looking for a config-file called like the process executable (e.g. ipy64.exe) which would have to be called ipy64.exe.config and reside in the IronPython installation directory. This is not very useful and maintainable when trying to run different scripts. If the referenced DLL is developed in-house the configuration mechanism could be adapted to allow for alternative/external configuration mechanisms but in many cases third-party assemblies are as-is and can't be changed.
If the example python script is executed the Helper class won't be able to find the required app settings and the result looks like:
Previous Solutions
An answer on Stack Overflow and the related blog post (which is based on another blog post) pointed me in the right direction by explaining how to implement IInternalConfigSystem in order to redirect all configuration requests to an arbitrary file (which can reside at a more convenient location like next to our IronPython script).
These solutions require to reference an additional .net assembly containing a class implementing IInternalConfigurationSystem. In my situation I wanted to avoid having to add another assembly due to some deployment constraints. Therefore a pure IronPython solution was the best approach.
Implementing IInternalConfigurationSystem in IronPython
The following snippet shows how to implement a configuration proxy in IronPython (based on the previous solutions):
When added to our initial IronPython script the ConfigurationProxy class can be used to redirect all configuration requests to a file called my.config located next to the IronPython script.
This will result in the correct output:
Full sample (VS2012 + pytools solution, tested with IronPython 2.7.3), possible updates can be found at our github repository.