Ironpython hosts and consumes the WCF Service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.