[IronPython] IronPython WCF service implementation problem

Ivan Porto Carrero ivan at flanders.co.nz
Mon Feb 16 10:10:20 PST 2009


I guess you're stuck with implementing a skeleton class in C# that you then
monkey patch (don't know the term for Python) to give the implementation in
python

2009/2/16 Jan R <janrou at gmail.com>

> Hi,
>
> I try to implement WCF services with a IronPython script. I try to
> implement the services for ICalculator in the WCF get started sample
> tutorial, see attachments below and
> http://msdn.microsoft.com/da-dk/library/ms751519(en-us).aspx<http://msdn.microsoft.com/da-dk/library/ms751519%28en-us%29.aspx>
> .
>
> Unfortunately I get the follwing Exception "InvalidOperationException" with
> the message:
>
> "In order to use one of the ServiceHost constructors that takes a service
> instance, the InstanceContextMode of the service must be set to
> InstanceContextMode.Single.  This can be configured via the
> ServiceBehaviorAttribute.  Otherwise, please consider using the ServiceHost
> constructors that take a Type argument."
>
> How do I set the appriated InstanceContextMode of the python class
> implemeting the services.
>
> Please help me out!
>
>
> Regards
> Jan Rouvillain
>
> Attachments.
>
> Program.cs:
>
>
> using System;
>
> using System.Collections.Generic;
>
> using System.Linq;
>
> using System.Text;
>
> using System.ServiceModel;
>
> using System.ServiceModel.Description;
>
> using IronPython;
>
> using IronPython.Hosting;
>
> using Microsoft.Scripting;
>
> using Microsoft.Scripting.Hosting;
>
> namespace Microsoft.ServiceModel.Samples
>
> {
>
> class Program
>
> {
>
> static void Main(string[] args)
>
> {
>
> Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
>
> // Se up python implmentation
>
> PythonRunner pyr = new PythonRunner();
>
> pyr.loadscript(@"..\..\..\Calculator\Calculator.py");
>
>
> pyr.run();
>
> ICalculator pyCalc = (ICalculator)pyr.getInstance("PyCalculator");
>
> ServiceHost selfHost = new ServiceHost( (object) pyCalc, baseAddress);
>
> try
>
> {
>
> // Step 3 of the hosting procedure: Add a service endpoint.
>
> selfHost.AddServiceEndpoint(
>
> typeof(ICalculator),
>
> new WSHttpBinding(),
>
> "CalculatorService");
>
> // Step 4 of the hosting procedure: Enable metadata exchange.
>
> ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
>
> smb.HttpGetEnabled = true;
>
> selfHost.Description.Behaviors.Add(smb);
>
> // Step 5 of the hosting procedure: Start (and then stop) the service.
>
>
> selfHost.Open();
>
> Console.WriteLine("The service is ready.");
>
> Console.WriteLine("Press <ENTER> to terminate service.");
>
> Console.WriteLine();
>
> Console.ReadLine();
>
> // Close the ServiceHostBase to shutdown the service.
>
>
> selfHost.Close();
>
> }
>
> catch (CommunicationException ce)
>
> {
>
> Console.WriteLine("An exception occurred: {0}", ce.Message);
>
> selfHost.Abort();
>
> }
>
> }
>
> }
>
> public class PythonRunner
>
> {
>
> Dictionary<String, Object> options;
>
> private ScriptEngine engine;
>
> private ScriptScope scope;
>
> private CompiledCode code;
>
> public PythonRunner()
>
> {
>
> InitializePythonEngine();
>
> }
>
> public void InitializePythonEngine()
>
> {
>
> options = new Dictionary<string, object>();
>
> options["DivisionOptions"] = PythonDivisionOptions.New;
>
> engine = Python.CreateEngine(options);
>
> }
>
> public void loadscript(string scriptPath)
>
> {
>
> ScriptSource script = engine.CreateScriptSourceFromFile(scriptPath);
>
>
> code = script.Compile();
>
> scope = engine.CreateScope();
>
> }
>
> public void SetVariable(string name, object value)
>
> {
>
> scope.SetVariable( name, value);
>
> }
>
> public void run()
>
> {
>
> try
>
> {
>
> code.Execute(scope);
>
> }
>
> catch (Exception exc)
>
> {
>
> Console.WriteLine("Python exception: " + exc.ToString());
>
> }
>
> }
>
> public Object getInstance(string className)
>
> {
>
> ObjectOperations ops = engine.Operations;
>
> object obj = scope.GetVariable(className);
>
> return (Object) ops.Call(obj);
>
> }
>
> }
>
> }
>
>
>
>
>
>
>
> CalaculatorService.cs:
>
>
>
> using System;
>
> using System.Collections.Generic;
>
> using System.Linq;
>
> using System.Text;
>
> using System.ServiceModel;
>
> namespace Microsoft.ServiceModel.Samples
>
> {
>
> [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
>
> public interface ICalculator
>
> {
>
>     // Step7: Create the method declaration for the contract.
>
>     [OperationContract]
>
>     double Add(double n1, double n2);
>
>
>
>     [OperationContract]
>
>     double Subtract(double n1, double n2);
>
>
>
>     [OperationContract]
>
>     double Multiply(double n1, double n2);
>
>
>
>     [OperationContract]
>
>     double Divide(double n1, double n2);
>
>     }
>
> }
>
>
>
> Calculator.py
>
>
>
> import clr
>
> clr.AddReference('PlatformService')
>
> 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
>
>
>
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20090216/b8345d93/attachment-0001.htm>


More information about the Users mailing list