Problem using IronPython to compile with .NET assemblies, especially the app.config aspect

I started learning IronPython for scripting against my .NET builds for specific tasks and special processes. I like the ease of development with IronPython and the small footprint compared to handling it with a .NET console app. However, I immediately ran into a roadblock with settings from the app.config file. The assemblies I plan to use require settings from the 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 proposed solutions were or were acceptable. You can modify the ipy.exe.config file. However, I would like to keep this as simple as possible and keep dependencies to a minimum. So that everyone can grab the IronPython script and run it without having to modify the ipy.exe.config file.

So, I decided to try this: I create a new application domain in the script and AppDomainSetup.ConfigurationFile points to the app.config file. Then I could call AppDomain.DoCallBack and pass in a delegate that has my logic. So below there is a script that has my try. Please note that I'm just learning IronPython / Python, so 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, the "c: \ MyApp" folder contains everything; exe, dll and app.config file. Hopefully the second appDomain will use MyApp.exe.config. The CallbackAction method is meant to contain code that the api from the .NET collectors will use to do some work. CallbackAction will be called via appDomain.DoCallBack. Well this is the part I ran into. When appDoming.DoCallBack is executed, I get System.Runtime.Serialization.SerializationException:

Unmanaged function pointers, dynamic methods, or methods outside of the delegate collector cannot be serialized.

I cannot fully understand this. My guess is that something is trying to be serialized through appDomains and that operation is failing. I can create CrossAppDomainDelegate and execute just fine.

test = CrossAppDomainDelegate(lambda: CallbackAction())
test()

      

Anyone have any ideas or recommendations? Basically, I need to have assemblies that I want to program in IronPython in order to be able to access the app.config file.

Thanks for your time and recommendations in advance.

btw I have IronPyhton 2.0.1 installed and I am using VS2008 Pro.

+2


source to share


1 answer


This is probably not the answer you are looking for, but ... Since this is for your own testing purposes, I would recommend installing Ironpython 2.7.1 and see if the problem goes away. Ironpython has had many improvements since 2.0.1.



0


source







All Articles