[IronPython] How to do the typeof( ) call on an IronPython implemented class in C#?

Jan R janrou at gmail.com
Tue Feb 17 12:59:55 CET 2009

Hello IronPython Experts.

How to do the typeof( ) call on an IronPython implemented class in C#?

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

ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

I want to replace the CalculatorService with a IronPython class
PyCalculator. Note the typeof(...).

 I have tried the following without success, in order to give an instance of
the IronPython class to the ServiceHost constructor :

ICalculator pyCalc = (ICalculator)pyr.getInstance("PyCalculator");
ServiceHost selfHost = new ServiceHost( (object) pyCalc, baseAddress);

The IronPython class is attached below. The code results in an exception,
because the attribute InstanceContextMode have to be set to

The WCF get-started sample is here:

Can anyone help me out?

Jan Rouvillain

IronPython code:

import clr
from Microsoft.ServiceModel.Samples import ICalculator

class PyCalculator(ICalculator):
  "Implements calculator services"

  def Add(self, a, b):
    print 'a ' + a + ' +  b ' + b
    return a+b

  def Subtract(self, a, b):
    print 'a ' + a + ' -  b ' + b
    return a-b

  def Multiply(self, a, b):
    print 'a ' + a + ' * b ' + b
    return a*b

  def Divide(self, a, b):
    print 'a ' + a + ' // b ' + b
    return a/b

C# interface declaration:

namespace Microsoft.ServiceModel.Samples
  [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
  public interface ICalculator
    // Step7: Create the method declaration for the contract.
    double Add(double n1, double n2);
    double Subtract(double n1, double n2);
    double Multiply(double n1, double n2);
    double Divide(double n1, double n2);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090217/74ee176e/attachment.html>

More information about the Ironpython-users mailing list