<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 12 (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;}
@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">What version of IronPython are you using?&nbsp; When I try this against 2.6.1 ScriptScope is &#8220;properly&#8221; returning null.&nbsp; &nbsp;&nbsp;I would recommend seeing how the lifetime
 issue stacks up w/ everything returning null.&nbsp;&nbsp;&nbsp; I think long term we need to provide an API that lets you provide the sponsors for hosting API objects but until we do that leasing your own objects may be futile unless there a whole lot of them in comparison
 to the hosting API objects.&nbsp; I actually&nbsp; haven&#8217;t done the leasing stuff myself either so I&#8217;m not sure I can quickly cook up an example.<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;"> users-bounces@lists.ironpython.com [mailto:users-bounces@lists.ironpython.com]
<b>On Behalf Of </b>mohammad mustaq<br>
<b>Sent:</b> Wednesday, August 11, 2010 7:30 AM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> Re: [IronPython] Query regarding Proxy Object lease period<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"><br>
I did override &quot;InitializeLifetimeService&quot; in MbrBase to return null. But in my example the &quot;scope&quot; object dies at first instance, so how would overriding &quot;InitializeLifetimeService&quot; in MbrBase help. Could you please modify my code to illustrate the right usage
 of ISponsor on the client side. <br>
<br>
My application will create objects periodically in the appdomain and execute methods implemented in IronPython. These methods may take around 20 minutes to execute. The whole application may run for a day or more. So I need to take this into consideration while
 implementing the lifetime service.<br>
<br>
I needed to confirm one thing. Do i need to provide the lease period for all objects created in the Appdomain. As per the code given below do i need to provide the lease period for &quot;code&quot;,&quot;scope&quot;,&quot;mbr&quot;,&quot;script&quot;, &quot;ops&quot; etc.<br>
Or is it only based on how long I expect the object to be alive.<br>
<br>
I am new to this concept of Appdomain hence my questions may sound silly.<o:p></o:p></p>
<div>
<p class="MsoNormal">On Tue, Aug 10, 2010 at 10:15 PM, Dino Viehland &lt;<a href="mailto:dinov@microsoft.com">dinov@microsoft.com</a>&gt; wrote:<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:#1F497D">The trick to keeping an object alive isn&#8217;t to call InitializeLifetimeService yourself but instead to return an appropriate lease.&nbsp; For
 example if you overrode InitializeLifetimeService in MbrBase to always return null your object will be kept alive forever.&nbsp; That will work fine as long as the app domain isn&#8217;t living for an extremely long time or that you have a limited number of these MBROs
 that you&#8217;re creating in the app domain.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:#1F497D">&nbsp;</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:#1F497D">We still have this problem to solve in the DLR as well where we&#8217;re still returning null.&nbsp; I think&nbsp; the correct solution is that we need
 to use ISponsor&#8217;s (ClientSPonsor provides an implementation of this) on the client side and then call ILease.Register so the object can call back to your sponser.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:11.0pt;color:#1F497D">&nbsp;</span><o:p></o:p></p>
<div style="border:none;border-left:solid windowtext 1.5pt;padding:0in 0in 0in 4.0pt;
border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color blue">
<div>
<div style="border:none;border-top:solid windowtext 1.0pt;padding:3.0pt 0in 0in 0in;
border-color:-moz-use-text-color -moz-use-text-color">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span style="font-size:10.0pt">From:</span></b><span style="font-size:10.0pt">
<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a> [mailto:<a href="mailto:users-bounces@lists.ironpython.com" target="_blank">users-bounces@lists.ironpython.com</a>]
<b>On Behalf Of </b>mohammad mustaq<br>
<b>Sent:</b> Tuesday, August 10, 2010 6:43 AM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> [IronPython] Query regarding Proxy Object lease period</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt">Hi,<br>
<br>
I posted this last week and I did not get a response.Hence posting it again.<br>
<br>
I have an application that creates a python engine in a new appdomain. If I do not specify a lease period I see that the objects are disconnected when I leave it idle for a period of 5 minutes. I used &quot;InitializeLifetimeService&quot; to keep the object alive forever
 but it did not work. The &quot;dlr-spec-hosting&quot; document mentions the following in page 93 under &quot;Current Issues&quot; section : &quot;Currently if you do not use a host for 15 min, it dies, and you lose your data. We've added InitializeLifetimeService on objects to cause
 them to stay alive forever, but we need to think through the design and the host controls here&quot;. Currently I renew the object using &quot;ILease&quot;. I do not know if this is the right thing to do. Could you please suggest me the right way to deal with this issue.
 I have provided the code below for your reference.<br>
<br>
Thanks,<br>
Mustaq<br>
<br>
using System;<br>
using Microsoft.Scripting;<br>
using IronPython.Hosting;<br>
using Microsoft.Scripting.Hosting;<br>
using System.Runtime.Remoting;<br>
using System.Runtime.Remoting.Lifetime;<br>
<br>
class Foo<br>
{<br>
&nbsp;&nbsp;&nbsp; public static void Main(string[] args)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AppDomain ad = AppDomain.CreateDomain(&quot;foo&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptEngine engine = Python.CreateEngine(ad);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; engine.Runtime.LoadAssembly(typeof(MbrBase).Assembly);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptSource code = engine.CreateScriptSourceFromString(@&quot;<br>
import MbrBase<br>
class C(MbrBase):<br>
&nbsp;&nbsp;&nbsp; def Test_C(self, log):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 0<br>
a = C()<br>
&quot;, SourceCodeKind.Statements);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptScope scope = engine.CreateScope();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ILease lifeTime = (ILease)scope.InitializeLifetimeService(); // Is this supposed to keep the object alive forever.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lifeTime.Renew(TimeSpan.FromDays(1)); // Provided a lease for one day.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; code.Execute(scope);&nbsp; // If the above lease is not mentioned then execution fails on this line after being inactive for 5 minutes.<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Trying to do it... {0}&quot;, <a href="http://AppDomain.CurrentDomain.Id" target="_blank">
AppDomain.CurrentDomain.Id</a>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MbrBase mbr = (MbrBase)scope.GetVariable(&quot;a&quot;);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string isSubClassCode = String.Format(&quot;issubclass({0},{1})&quot;, &quot;C&quot;, &quot;MbrBase&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptSource script = engine.CreateScriptSourceFromString(isSubClassCode, SourceCodeKind.Expression);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool result = (bool)script.Execute(scope);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (result == true)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectOperations ops = engine.Operations;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectHandle subClass = scope.GetVariableHandle(&quot;C&quot;);&nbsp;&nbsp;&nbsp; // get back a handle<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectHandle instance = ops.Call(subClass, new ObjectHandle[0]);&nbsp; // invoke the handle to create an instance<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbr = instance.Unwrap() as MbrBase;&nbsp; // now we know we have an MBR and we can unwrap it to our local side<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ObjectHandle temp = ops.GetMember(instance, &quot;Test_C&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object log = null;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ops.Call(temp, log);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbr.DoItVirtually();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mbr.DoIt();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.ReadKey();<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
public class MbrBase : MarshalByRefObject<br>
{<br>
&nbsp;&nbsp;&nbsp; public virtual void DoItVirtually()<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Did it virtually {0}&quot;, <a href="http://AppDomain.CurrentDomain.Id" target="_blank">
AppDomain.CurrentDomain.Id</a>);<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; public void DoIt()<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Did it {0}&quot;, <a href="http://AppDomain.CurrentDomain.Id" target="_blank">
AppDomain.CurrentDomain.Id</a>);<br>
&nbsp;&nbsp;&nbsp; }<br>
}<o:p></o:p></p>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">&nbsp;<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</div>
</body>
</html>