I started looking into IronPython to develop scripts against my .NET assemblies for certain tasks and ad-hoc processes. I like the ease of development with IronPython and the small footprint compared to handling this with a .NET console application project. However, I immediately ran into a roadblock with settings from app.config file. The assemblies I am planning to use require settings from app.config file such as database connection string, application settings, etc. I saw this question on SO How to use IronPython with App.Config. However, from what I gather and assume, none of the suggested solutions worked or were acceptable. Modifying ipy.exe.config file has a potential. However, I would like to keep this as simple as possible and minimize the dependencies. So that anyone can grab the IronPython script and run it without having to modify ipy.exe.config file.
So I decided to try the following: I create a new application domain in the script and have AppDomainSetup.ConfigurationFile point to the app.config file. Then I could call AppDomain.DoCallBack and pass a delegate that has my logic. So below is the script that has my attempt. Note that I am just learning IronPython/Python so please keep that in mind.
import clr
import sys
sys.path.append(r"C:\MyApp");
clr.AddReference("System")
clr.AddReference("Foo.dll")
from System import *
from System.IO import Path
from Foo import *
def CallbackAction():
print "Callback action"
baseDirectory = r"C:\MyApp"
domainInfo = AppDomainSetup()
domainInfo.ApplicationBase = baseDirectory
domainInfo.ConfigurationFile = Path.Combine(baseDirectory,"MyApp.exe.config")
appDomain = AppDomain.CreateDomain("Test AppDomain",AppDomain.CurrentDomain.Evidence,domainInfo)
appDomain.DoCallBack(CallbackAction) #THIS LINE CAUSES SERIALIZATION EXCEPTION
Console.WriteLine("Hit any key to exit...")
Console.ReadKey()
In the above code, "c:\MyApp" folder contains everything; exe, dlls, and the app.config file. Hopefully the second appDomain will use MyApp.exe.config. CallbackAction method is intended to contain the code that will use the api from the .NET assemblies to do some work. CallbackAction will be invoked via appDomain.DoCallBack. Well, this is the part I am having a problem. When appDoming.DoCallBack is executed, I get a System.Runtime.Serialization.SerializationException:
Cannot serialize delegates over unmanaged function pointers, dynamic methods or methods outside the delegate creator's assembly.
I can't make a complete sense out of this. I assume that something is being attempted to be serialized across appDomains and that operation is failing. I can create a CrossAppDomainDelegate and execute it just fine.
test = CrossAppDomainDelegate(lambda: CallbackAction())
test()
So does anyone have any ideas or recommendations? Basically, I need to have the assemblies that I want to code against in IronPython to have access to the app.config file.
Thanks for your time and recommendations in advance.
btw I have IronPyhton 2.0.1 installed and am using VS2008 Pro.