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

本文共 2177 字,大约阅读时间需要 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 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 "IronPython WCF服务: 你的输入内容是: %s" % value
sh = 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 clr
clr.AddReference('System.ServiceModel')
from System import Console
from System.ServiceModel import (ChannelFactory, BasicHttpBinding, EndpointAddress)
clr.AddReference('TestServiceInterface')
from TestServiceInterface import ImyService
mycf = 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/

你可能感兴趣的文章
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
Node+Express连接mysql实现增删改查
查看>>