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,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
137 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
3月前
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
104 32
|
3月前
|
网络安全 Windows
Windows server 2012R2系统安装远程桌面服务后无法多用户同时登录是什么原因?
【11月更文挑战第15天】本文介绍了在Windows Server 2012 R2中遇到的多用户无法同时登录远程桌面的问题及其解决方法,包括许可模式限制、组策略配置问题、远程桌面服务配置错误以及网络和防火墙问题四个方面的原因分析及对应的解决方案。
226 4
|
3月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
63 11
|
4月前
|
弹性计算 关系型数据库 网络安全
阿里云国际版无法连接和访问Windows服务器中的FTP服务
阿里云国际版无法连接和访问Windows服务器中的FTP服务
|
4月前
|
边缘计算 安全 网络安全
|
4月前
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
124 9
|
Windows C#
Windows Service的安装卸载 和 Service控制
原文 Windows Service的安装卸载 和 Service控制 本文内容包括如何通过C#代码安装Windows Service(exe文件,并非打包后的安装文件)、判断Service是否存在、获得Service状态及启动停止Service。
909 0
|
4天前
|
安全 网络安全 数据安全/隐私保护
Windows Server 2025 Active Directory 重置用户密码
密码重置是管理员日常任务之一,用户因忘记或多次输错密码导致账户锁定时需进行重置。本文介绍在Active Directory服务器上重置密码的三种方法。
|
2月前
|
安全 关系型数据库 MySQL
Windows Server 安装 MySQL 8.0 详细指南
安装 MySQL 需要谨慎,特别注意安全配置和权限管理。根据实际业务需求调整配置,确保数据库的性能和安全。
250 9