[IronPython] IronPython WCF service implementation problem
Jan R
janrou at gmail.com
Mon Feb 16 18:47:16 CET 2009
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.
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090216/bac4f4ce/attachment.html>
More information about the Ironpython-users
mailing list