When I started to learn ironpython, I mentioned in the article that "ironpython 2.6 provides the new clrtype, allowing programmers to provide CLR type information such as property and attribute with pure ironpython code. In this way, ironpython code can be seamlessly integrated with sliverlight, WCF, and other frameworks ." We will use clrtype to see how to host and consume the WCF Service. An interface needs to be defined for the WCF contract, which is not currently supported by ironpython. Therefore, use C # To define a WCF contract:
Using system;
Using system. Collections. Generic;
Using system. servicemodel;
Namespace testserviceinterface
{
/// <Summary>
/// Description of myclass.
/// </Summary>
[Servicecontract]
Public interface imyservice
{
[Operationcontract]
String getdata (INT value );
}
}
Compile the Assembly testserviceinterface. dll and implement the WCF Service mywcfservice. myservice in ironpython. The Code is as follows:
Import CLR
Import clrtype
CLR. addreference ('System. servicemodel ')
CLR. addreference ('testserviceinterface ')
From testserviceinterface import imyservice
From system import console, Uri
From system. servicemodel import (servicehost, basichttpbinding, servicebehaviorattribute, instancecontextmode)
Servicebehavior = clrtype. Attribute (servicebehaviorattribute)
Class myservice (imyservice ):
_ Metaclass _ = clrtype. clrclass
_ Clrnamespace = "mywcfservice"
_ Clrclassattribs = [servicebehavior (instancecontextmode = instancecontextmode. Single)]
Def getdata (self, value ):
Return R "ironpython WCF Service: your input is: % s" % value
SH = servicehost (myservice (), Uri ("http: // localhost: 7000/mywcfservice % 22 ))
Sh. addserviceendpoint (
CLR. getclrtype (imyservice ),
Basichttpbinding (),
"")
Sh. open ()
Console. writeline ("press <enter> to terminate \ n ")
Console. Readline ()
Sh. Close ()
Here I used a clrtype module, code to participate in ironpython sample project, can be downloaded from here: http://ironpython.codeplex.com/Release/ProjectReleases.aspx? Releaseid = 12482. The first example is clrtype. Copy it from here and put it in our py directory. An Attribute must be added to the class myservice.Instancecontextmode. SingleServicebehavior.
Let's write a test client code:
Import CLR
CLR. addreference ('System. servicemodel ')
From system import Console
Import system. servicemodel
CLR. addreference ('testserviceinterface ')
From testserviceinterface import imyservice
Mycf = system. servicemodel. channelfactory [imyservice] (
System. servicemodel. basichttpbinding (),
System. servicemodel. endpointaddress ("http: // localhost: 7000/mywcfservice % 22 ))
Wcfcli = mycf. createchannel ()
The returned result of print R "ironpython WCF Service is \ n % s" % wcfcli. getdata (11)
Console. writeline ("press <enter> to terminate \ n ")
Console. Readline ()
The running result is as follows:
This is the completion of a basic WCF Service example. A problem in this example is that you cannot use a configuration file to store the configuration information of WCF. You can only configure the WCF Service in code mode.
In addition, several articles on ironpython combined with Entity Framework are linked, and the clrtype module is also used.
Entity Framework, ironpython and Podo's-can it be done? -Part 1
Entity Framework, ironpython and Podo's-can it be done? -Part 2
Entity Framework, ironpython and Podo's-can it be done? -Part 3
Entity Framework, ironpython and Podo's-can it be done? -Part 4