前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IronPython 承载和消费WCF服务

IronPython 承载和消费WCF服务

作者头像
张善友
发布2018-01-22 10:58:49
8940
发布2018-01-22 10:58:49
举报
文章被收录于专栏:张善友的专栏张善友的专栏

是开始学习IronPython 的时候了文章里谈到了“IronPython 2.6提供了新特性clrtype,允许程序员用纯IronPython代码提供property、attribute等CLR类型信息。这样IronPython代码就可以无缝地与Sliverlight、WCF等框架集成。”我们就用clrtype来看看怎么承载WCF服务和消费WCF服务。WCF的契约需要定义接口,这是目前IronPython 尚未支持的功能,所以我们先用C#定义个一个WCF的契约:

代码语言:js
复制
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);? 
??? }? 
}

编译成一个程序集TestServiceInterface.dll, 然后在IronPython中实现WCF服务myWcfService.myService,代码如下:

代码语言:js
复制
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服务: 你的输入内容是: %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() 

这里用到了一个clrtype模块, 代码参加IronPython的Sample工程,可以从这里下载:http://ironpython.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=12482 ,第一个例子就是ClrType ,从这里拷贝出来放到我们的py目录下。类myService 必须加一个attribute InstanceContextMode.Single ServiceBehavior 。

我们再来写个测试的客户端代码:

代码语言:js
复制
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()? 
print r"IronPython WCF 服务的返回结果是:\n%s" % wcfcli.GetData(11) 
Console.WriteLine("Press <ENTER> to terminate\n")? 
Console.ReadLine()

运行起来可以得到的结果如下:

image
image

这样一个基本的WCF服务示例就完成了,示例有个问题是没法使用配置文件来存储WCF的配置信息,只能通过代码方式对WCF服务进行配置。

另外链接几篇IronPython结合Entity Framework的文章,其中也使用到了clrtype模块。

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

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-01-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com