Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  489 随笔 :: 0 文章 :: 417 评论 :: 70万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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


 

 

posted on   Lei Zhang的博客  阅读(1612)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示