博客
关于我
IronPython 承载和消费WCF服务
阅读量:447 次
发布时间:2019-03-06

本文共 2129 字,大约阅读时间需要 7 分钟。

IronPython 2.6引入了一个名为clrtype的新特性,这使得程序员可以使用纯IronPython代码来提供CLR(Common Language Runtime,公共语言运行时)类型的属性信息。这种功能使得IronPython代码能够无缝地与Sliverlight、WCF(Windows Communication Foundation)等框架集成。在本文中,我们将使用clrtype来探讨如何承载和消费WCF服务。

首先,我们需要定义一个WCF服务的接口。由于IronPython尚未直接支持接口定义,我们将使用C#来定义一个接口:

using System;using System.Collections.Generic;using System.ServiceModel;namespace TestServiceInterface{    ///     /// Description of MyClass.    ///     [ServiceContract]    public interface ImyService    {        [OperationContract]        string GetData(int value);    }}

接下来,我们将使用IronPython和clrtype来实现这个接口,创建一个WCF服务。以下是实现代码:

import clrimport clrtypeclr.AddReference('System.ServiceModel')clr.AddReference('TestServiceInterface')from TestServiceInterface import ImyServicefrom System import Console, Urifrom 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 "IronPython WCF服务: 你的输入内容是: %s" % valuesh = ServiceHost(myService(), Uri("http://localhost:8000/ServiceHost"))sh.AddServiceEndpoint(    clr.GetClrType(ImyService),    BasicHttpBinding(),    "")sh.Open()Console.WriteLine("Press 
to terminate\n")Console.ReadLine()sh.Close()

此外,我们需要编写一个客户端代码来测试这个WCF服务:

import clrclr.AddReference('System.ServiceModel')from System import Consolefrom System.ServiceModel import (ChannelFactory, BasicHttpBinding, EndpointAddress)clr.AddReference('TestServiceInterface')from TestServiceInterface import ImyServicemycf = ChannelFactory[ImyService](    BasicHttpBinding(),    EndpointAddress("http://localhost:8000/ServiceHost"))wcfcli = mycf.CreateChannel()print("IronPython WCF 服务的返回结果是:\n%s" % wcfcli.GetData(11))Console.WriteLine("Press 
to terminate\n")Console.ReadLine()

需要注意的是,由于IronPython不支持通过配置文件来存储WCF服务的配置信息,我们只能通过代码来配置WCF服务。

此外,IronPython与 Entity Framework 的结合也使用了clrtype模块,这可以作为进一步探索的方向。

转载地址:http://ejifz.baihongyu.com/

你可能感兴趣的文章
Objective-C实现MATLAB中Filter函数功能(附完整源码)
查看>>
Objective-C实现matrix chainorder矩阵链顺序算法(附完整源码)
查看>>
Objective-C实现matrix exponentiation矩阵求幂算法(附完整源码)
查看>>
Objective-C实现MatrixMultiplication矩阵乘法算法 (附完整源码)
查看>>
Objective-C实现max non adjacent sum最大非相邻和算法(附完整源码)
查看>>
Objective-C实现max subarray sum最大子数组和算法(附完整源码)
查看>>
Objective-C实现max sum sliding window最大和滑动窗口算法(附完整源码)
查看>>
Objective-C实现MaxHeap最大堆算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(Brute Force蛮力解决方案)算法(附完整源码)
查看>>
Objective-C实现MaximumSubarray最大子阵列(动态规划解决方案)算法(附完整源码)
查看>>
Objective-C实现maxpooling计算(附完整源码)
查看>>
Objective-C实现max_difference_pair最大差异对算法(附完整源码)
查看>>
Objective-C实现max_heap最大堆算法(附完整源码)
查看>>
Objective-C实现MD5 (附完整源码)
查看>>
Objective-C实现md5算法(附完整源码)
查看>>
Objective-C实现MeanSquareError均方误差算法 (附完整源码)
查看>>
Objective-C实现median filter中值滤波器算法(附完整源码)
查看>>
Objective-C实现memcmp函数功能(附完整源码)
查看>>
Objective-C实现memcpy函数功能(附完整源码)
查看>>
Objective-C实现memoization优化技术算法(附完整源码)
查看>>