您的位置:首页 > 其它

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,完成后的配置文件如下:

<?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 微软云计算平台技术详解》 电子工业出版社
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐