Windows Azure Cloud Service (18) 基于Input Endpoint通过Worker Role发布WCF服务

简介:

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 微软云计算平台技术详解》 电子工业出版社


 

 


本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/archive/2012/03/04/2379210.html,如需转载请自行联系原作者
目录
相关文章
|
10月前
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
204 32
|
10月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
160 11
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
209 1
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
114 0
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
109 0
|
测试技术
WindowsService的调试方法
本人转载:http://www.cnblogs.com/xiebin1986/archive/2011/12/15/2288893.html 调试WindowsService,以前用过一个附加到进程的方法,还是挺麻烦的,需要先安装并启动服务,后来找到了一个更好的方法,方法如下: 在Service1.
829 0
|
4月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 5 月更新)
726 2
|
6月前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
317 4
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
|
6月前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
371 4
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
|
3月前
|
Linux 虚拟化 iOS开发
Windows Server 2022 OVF (2025 年 6 月更新) - VMware 虚拟机模板
Windows Server 2022 OVF (2025 年 6 月更新) - VMware 虚拟机模板
234 6
Windows Server 2022 OVF (2025 年 6 月更新) - VMware 虚拟机模板