<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">Are you creating the tracing delegate in the remote app domain?&nbsp; You should be able to create a class which derives from MarshalByRefObject and then create
 an instance of that object in the remote domain.&nbsp; You can then call a method on that MBRO which does the ScriptEngine.SetTrace() call (you can safely pass the script engine instance across when you create the MBRO).&nbsp; If you want to marshal the results back
 to your main app domain you can also create an MBRO in your local domain that you pass through when you construct the remote MBRO and then you can call some method which passes strings instead of unmarshallable objects.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"> ironpython-users-bounces&#43;dinov=microsoft.com@python.org [mailto:ironpython-users-bounces&#43;dinov=microsoft.com@python.org]
<b>On Behalf Of </b>Alan Macdonald<br>
<b>Sent:</b> Thursday, January 12, 2012 1:34 AM<br>
<b>To:</b> ironpython-users@python.org<br>
<b>Subject:</b> [Ironpython-users] SerializationException from IronPython.Runtime.Types.PythonType<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">Hello,<br>
<br>
I am experiencing an exception when tracing and would appreciate some guidance.<br>
<br>
I am hosting IronPython 2.7.1 in a .Net 4 C# host.&nbsp; The host creates a new app domain to run IronPython scripts in.&nbsp; At the moment due to some security exceptions I've given the new app domain unrestricted permissions (I will try and reduce this later).&nbsp; When
 the python script is executed it runs via an asynchronous delegate and when complete catches any exceptions during the running of the script.&nbsp; This was all working fine and if the python script has an error in it then the exception is caught in the async callback
 method.<br>
<br>
However I have since added tracing via a call to ScriptEngine.SetTrace(myDelegate) so I can allow stepping through the script one line at a time (based on
<a href="http://devhawk.net/tag/debugger/">http://devhawk.net/tag/debugger/</a>).&nbsp; When the script has no errors it executes fine with tracing enabled or disabled.&nbsp; However if the script has an error in it and tracing is enabled then trying to catch and format
 the exception in the async delegate callback causes a SerializationException on&nbsp; IronPython.Runtime.Types.PythonType.&nbsp; When tracing is not enabled the exception is caught and formatted successfully.<br>
<br>
I tried to disable tracing in the catch block before formatting the exception but this makes no difference.&nbsp; I understand that this is related to sending the exception information from one app domain to another but I'm not sure what I can do to get round it
 and what extra information it is trying to send when tracing is enabled.&nbsp; My tracing delegate does nothing with exceptions.<br>
<br>
Code that creates the new app domain and starts the async call to execute the method for the object in the new app domain:<br>
public void Run()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (newDomainInstance == null)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NewAppDomain();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute method = newDomainInstance.ExecuteCode;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method.BeginInvoke(RunComplete, method);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
<br>
<br>
The async callback is the following code:<br>
<br>
&nbsp;private void RunComplete(IAsyncResult cookie)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Complete = true;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute method = (Execute)cookie.AsyncState;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method.EndInvoke(cookie);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnExecutionSuccess();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (AppDomainUnloadedException adue)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Do nothing because we've deliberately pulled the app domain out from under it.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception ex)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; engine.Value.SetTrace(null);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String extractedMessage = engine.Value.GetService&lt;ExceptionOperations&gt;().FormatException(ex);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnExecutionError(new Exception(extractedMessage));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
<br>
The exception occurs on String extractedMessage = engine.Value.GetService&lt;ExceptionOperations&gt;().FormatException(ex);<br>
<br>
Exception stacktrace:<br>
System.Runtime.Serialization.SerializationException was unhandled<br>
&nbsp; Message=Type 'IronPython.Runtime.Types.PythonType' in Assembly 'IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1' is not marked as serializable.<br>
&nbsp; Source=mscorlib<br>
&nbsp; StackTrace:<br>
&nbsp;&nbsp;&nbsp; Server stack trace: <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter,
 SerializationBinder binder)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage..ctor(IMethodReturnMessage mrm)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage.SmuggleIfPossible(IMessage msg)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage&amp; smuggledMrm)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)<br>
&nbsp;&nbsp;&nbsp; Exception rethrown at [0]: <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Threading.Thread.InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Messaging.AsyncReplySink.SyncProcessMessage(IMessage reqMsg)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Runtime.Remoting.Channels.ADAsyncWorkItem.FinishAsyncWork(Object stateIgnored)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Threading.ThreadPoolWorkQueue.Dispatch()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()<br>
&nbsp; InnerException: <br>
<br>
<br>
Any help would be appreciated.&nbsp; If it helps I can show more code or if anything is unclear please let me know. I didn't want to make it too long and include unneccessary details.<br>
<br>
Thanks for your time.<br>
<br>
Alan<br>
<br>
<o:p></o:p></p>
</div>
</div>
</body>
</html>