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

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

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

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,代码如下:

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(" ) )

sh.AddServiceEndpoint( 

       clr.GetClrType(ImyService), 

        BasicHttpBinding(),

       "") 

sh.Open() 

Console.WriteLine("Press <ENTER> to terminate\n") 

Console.ReadLine() 

sh.Close()

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

 

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

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("

wcfcli = mycf.CreateChannel() 

print r"IronPython WCF 服务的返回结果是:\n%s" % wcfcli.GetData(11)

Console.WriteLine("Press <ENTER> to terminate\n") 

Console.ReadLine()

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

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

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

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

你可能感兴趣的文章
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>