Windows Azure Cloud Service (18) 基于Input Endpoint通过Worker Role发布WCF服务
2012-03-04 17:06
1421 查看
《Windows Azure Platform 系列文章目录》
由于Input Endpoint可以通过Hosted Service URL直接访问,所示可以利用这个特点基于Worker Role寄宿一个使用NET.TCP协议的WCF服务。
注:对于WCF服务不了解的网友可以参考 http://www.cnblogs.com/artech
首先在Visual Studio中创建一个Windows Azure项目并加入一个Worker Role。然后,在这个solution中添加两个项目,分别是WCF服务契约的项目和测试用控制台项目。而WCF服务的具体逻辑则在Worker Role项目中实现。接下来完成一个简单的EchoService功能,即将客户端传入的字符串加入时间信息再返回给客户端。
接下来将这个WCF服务寄宿在Worker Role中。首先需要设定一个对外的Endpoint。打开Endpoint界面,由于要用户能够从Internet访问这个服务,所以创建一个Input Endpoint,并且将Endpoint的类型设置为TCP,这样就可以支持NET.TCP的WCF通信。最后,将端口号指定为3030.
在配置文件中加入WCF寄宿信息。由于这个服务将会被部署到Windows Azure一个已经创建好的Hosted Service中,所以其对外的URL是事先知道的。因此就可以直接在配置文件中指定这个服务的发布地址,例如:net.tcp://leizhang.cloudapp.net:3030/EchoService。然后使用NET.TCP Binding,讲安全级别设置为None,完成后的配置文件如下:
回到Worker Role的代码中,在Run方法中启动WCF服务,并且在OnStop方法中停止WCF服务。完成后的代码如下所示:
最后,创建一个简单的客户端程序来访问这个服务。具体的操作步骤这里就不详细介绍了,直接看一下对应的配置文件。如下所示,在WCF的配置部分指定了Hosted Service上的WCF地址和端口号。
本文摘自:徐子岩著的《实战Windows Azure 微软云计算平台技术详解》 电子工业出版社
由于Input Endpoint可以通过Hosted Service URL直接访问,所示可以利用这个特点基于Worker Role寄宿一个使用NET.TCP协议的WCF服务。
注:对于WCF服务不了解的网友可以参考 http://www.cnblogs.com/artech
首先在Visual Studio中创建一个Windows Azure项目并加入一个Worker Role。然后,在这个solution中添加两个项目,分别是WCF服务契约的项目和测试用控制台项目。而WCF服务的具体逻辑则在Worker Role项目中实现。接下来完成一个简单的EchoService功能,即将客户端传入的字符串加入时间信息再返回给客户端。
接下来将这个WCF服务寄宿在Worker Role中。首先需要设定一个对外的Endpoint。打开Endpoint界面,由于要用户能够从Internet访问这个服务,所以创建一个Input Endpoint,并且将Endpoint的类型设置为TCP,这样就可以支持NET.TCP的WCF通信。最后,将端口号指定为3030.
在配置文件中加入WCF寄宿信息。由于这个服务将会被部署到Windows Azure一个已经创建好的Hosted Service中,所以其对外的URL是事先知道的。因此就可以直接在配置文件中指定这个服务的发布地址,例如:net.tcp://leizhang.cloudapp.net:3030/EchoService。然后使用NET.TCP Binding,讲安全级别设置为None,完成后的配置文件如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.diagnostics> ...... </system.diagnostics> <system.serviceModel> <services> <service name="EchoService.Service.EchoService"> <endpoint address="net.tcp://leizhang.cloudapp.net:3030/EchoService" binding="netTcpBinding" contract="EchoService.Contract.IEchoService" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <bindings> <netTcpBinding> <binding> <security mode="None" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
回到Worker Role的代码中,在Run方法中启动WCF服务,并且在OnStop方法中停止WCF服务。完成后的代码如下所示:
public class WorkerRole : RoleEntryPoint { private ServiceHost _host; public override void Run() { //This is a sample worker implementation. Replace with your logic. Trace.TraceInformation("EchoSerive.Service entry point called"); //host the wcf service _host = new ServiceHost(typeof(EchoService)); _host.Opened += (sender,e)=> { Trace.TraceInformation("WCF opened at {0}",_host.Description.Endpoints [0].Address); }; _host.open(); while (true) { Thread.Sleep(10000); Trace.TraceInformation("Running .."); } } public override bool OnStart() { //Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit=12; CkoudStorageAccount.SetConfigurationSettingPublisher((configName configSetter)=> { configSetter(RoleEnvionment.GetConfigurationSettingValue(configName)); }); return base.OnStart(); } public override void OnStop() { if(_host!=null) _host.Close(); base.OnStop(); } }
最后,创建一个简单的客户端程序来访问这个服务。具体的操作步骤这里就不详细介绍了,直接看一下对应的配置文件。如下所示,在WCF的配置部分指定了Hosted Service上的WCF地址和端口号。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> <system.serviceModel> <client> <endpoint name="EchoService" address="net.tcp://leizhang.cloudapp.net:3030/EchoService" binding="netTcpBinding" contract="EchoService.Contract.IEchoService" /> </service> </client> <bindings> <netTcpBinding> <binding> <security mode="None" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>
本文摘自:徐子岩著的《实战Windows Azure 微软云计算平台技术详解》 电子工业出版社
相关文章推荐
- Windows Azure Cloud Service (19) 动态指定WCF的发布地址
- [New Portal]Windows Azure Web Site (5) 如何选择Windows Azure托管服务的类型?WebSite, Cloud Service还是Virtual Machine
- Windows Azure Cloud Service (40) 使用VS2013的publishSettings文件,发布Cloud Service
- Windows Azure Cloud Service (12) PaaS之Web Role, Worker Role, Azure Storage Queue(下)
- [SDK2.2]Windows Azure Cloud Service (35) 使用VS2013发布Azure Cloud Service
- Visual Studio2015发布web站点到windows azure Cloud Service(PAAS)
- Windows Azure Cloud Service (20) 使用Internal Endpoint实现Role的内部通信
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
- Windows Azure Cloud Service (41) 修改云服务IIS托管管道模式为4.0经典模式
- Windows Azure Cloud Service (11) PaaS之Web Role, Worker Role(上)
- Preparing for Application and Service Deployment-Adding an HTTPS Endpoint to a Windows Azure Cloud
- Windows Azure Cloud Service (21) 通过远程桌面功能访问Windows Azure 计算节点
- windows azure Vm、cloud service、web application 如何选择可用的服务
- Windows Azure SDK 1.5、Windows Azure Tools for Microsoft Visual Studio 2010和新的服务管理功能发布了
- 云服务系列:Windows Azure SDK for .NET(2012 年 6 月发布的版本)的最新消息
- 通过纯代码方式发布WCF服务
- 宣布 Windows Azure 通过 PCI DSS 合规性验证并且 ISO 认证范围扩大,同时正式发布 Windows Azure Hyper-V 恢复管理器和其他更新功能
- WCF 服务 宿主在windows service中的Demo
- C# windows服务:通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
- Windows Azure Cloud Service (14) 使用Windows Azure诊断收集日志记录数据