<span class="Apple-style-span" style="border-collapse: collapse; "><div>Hello IronPython Experts.</div><div><br></div><div><br></div><div>How to do the typeof( ) call on an IronPython implemented class in C#?<br></div><div>
<br></div><div>The need to do a typeof(..) comes from implementing services in Windows Communication Foundation (WCF) in IronPython. The WCF get-statrted sample uses the overloaded constructor of the ServiceHost class that takes a System.Type:</div>
<div><br></div><div><span style="font-family: Verdana; font-size: 11px; line-height: 15px; "><pre style="background-color: rgb(221, 221, 221); border-top-color: rgb(200, 205, 222); border-top-style: solid; border-top-width: 1px; padding-left: 5px; padding-right: 5px; padding-top: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; display: block; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 105%; color: rgb(0, 0, 102); overflow-x: hidden; overflow-y: hidden; white-space: pre-wrap; word-wrap: break-word; ">
ServiceHost selfHost = <span style="color: blue; ">new</span> ServiceHost(typeof(CalculatorService), baseAddress);</pre></span></div><div><br></div><div>I want to replace the CalculatorService with a IronPython class PyCalculator. Note the typeof(...).</div>
<div><br></div><div>&nbsp;I have tried the following without success, in order to give an instance of the IronPython class to the ServiceHost constructor :</div><div><br></div><div><div class="Ih2E3d" style="color: rgb(80, 0, 80); ">
<div><span style="border-collapse: collapse; font-family: Verdana; font-size: 11px; line-height: 15px; "><span style="color: rgb(0, 0, 102); font-size: 12px; white-space: pre-wrap; "><font color="#2b91af" size="2"><font color="#2b91af" size="2">ICalculator</font></font><font size="2"> pyCalc = (</font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ICalculator</font></font><font size="2">)pyr.getInstance(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;PyCalculator&quot;</font></font><font size="2">);<span style="font-size: 12px; "><font color="#2b91af" size="2"><font color="#2b91af" size="2"></font></font></span></font></span></span></div>
<div><span style="border-collapse: collapse; font-family: Verdana; font-size: 11px; line-height: 15px; "><span style="color: rgb(0, 0, 102); font-size: 12px; white-space: pre-wrap; "><font size="2"><span style="font-size: 12px; "><font color="#2b91af" size="2"><font color="#2b91af" size="2">ServiceHost</font></font><font size="2"> selfHost = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ServiceHost</font></font><font size="2">( (</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">object</font></font><font size="2">) pyCalc, baseAddress);</font></span></font></span></span></div>
<div><span style="border-collapse: collapse; color: rgb(0, 0, 102); font-family: Verdana; line-height: 15px; white-space: pre-wrap; "><br></span></div></div><div><span style="border-collapse: collapse; color: rgb(0, 0, 102); font-family: Verdana; line-height: 15px; white-space: pre-wrap; ">The IronPython class is attached below. The code results in an exception, because t<span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: arial; line-height: normal; white-space: normal; ">he attribute&nbsp;<span style="color: rgb(0, 0, 102); font-family: Verdana; font-size: 12px; line-height: 15px; white-space: pre-wrap; ">InstanceContextMode have to be set to InstanceContextMode.Single.</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 102); font-family: Verdana; font-size: 12px; line-height: 15px; white-space: pre-wrap;"><br></span></div><div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 102); font-family: Verdana; font-size: 12px; line-height: 15px; white-space: pre-wrap;">The WCF get-started sample is here: <span class="Apple-style-span" style="border-collapse: collapse; color: rgb(0, 0, 0); font-family: arial; font-size: 13px; line-height: normal; white-space: normal; "><a href="http://msdn.microsoft.com/da-dk/library/ms734712(en-us).aspx">http://msdn.microsoft.com/da-dk/library/ms734712(en-us).aspx</a></span></span></div>
</div><div><br></div><div>Can anyone help me out?</div><div><br></div><div><br></div><div>Regards</div><div>Jan Rouvillain</div><div><br></div><div>IronPython code:</div><div><br></div><div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">import clr</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">clr.AddReference(&#39;CalculatorServer&#39;)</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">from Microsoft.ServiceModel.Samples import ICalculator</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);"><br></span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">class PyCalculator(ICalculator):</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp;&quot;Implements calculator services&quot;</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);"><br></span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp;def Add(self, a, b):</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;print &#39;a &#39; + a + &#39; + &nbsp;b &#39; + b</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;return a+b</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);"><br></span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp;def Subtract(self, a, b):</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;print &#39;a &#39; + a + &#39; - &nbsp;b &#39; + b</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;return a-b</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);"><br></span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp;def Multiply(self, a, b):</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;print &#39;a &#39; + a + &#39; * b &#39; + b</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;return a*b</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);"><br></span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp;def Divide(self, a, b):</span></div>
<div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;print &#39;a &#39; + a + &#39; // b &#39; + b</span></div><div><span class="Apple-style-span" style="color: rgb(102, 0, 204);">&nbsp;&nbsp; &nbsp;return a/b</span></div>
<div><br></div><div>C# interface declaration:</div><div><br></div><div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">namespace Microsoft.ServiceModel.Samples</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">{</span></div>
<div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp;[ServiceContract(Namespace = &quot;<a href="http://Microsoft.ServiceModel.Samples">http://Microsoft.ServiceModel.Samples</a>&quot;)]</span></div><div>
<span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp;public interface ICalculator</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp;{</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;// Step7: Create the method declaration for the contract.</span></div>
<div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;[OperationContract]</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;double Add(double n1, double n2);</span></div>
<div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;[OperationContract]</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;double Subtract(double n1, double n2);</span></div>
<div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;[OperationContract]</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;double Multiply(double n1, double n2);</span></div>
<div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;[OperationContract]</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp; &nbsp;double Divide(double n1, double n2);</span></div>
<div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">&nbsp;&nbsp;}</span></div><div><span class="Apple-style-span" style="color: rgb(153, 51, 153);">}</span></div><div><br></div></div></div></span>