技术开发 频道

Azure Services探索:在Azure服务中增加WCF服务

  现在可以点击Worker Role项目,然后选择添加服务引用(Add Service Reference)

 

  这里有个小小的技巧,因为现在在开发调试环境,WCF服务并没有运行,这时你可以先在地址框输入http://localhost/myMessageLogger.svc 然后点击Discover 按钮,然后地址框中的端口号自动变了,这时是本机的ASP.NET Web Development Server启动了产生的新的端口号,我们现在只是要增加一个引用,1030端口不是Azure运行WCF的端口,如果有端口应该是我们刚刚在错误页面看到的5100。

  增加好WCF服务引用之后,要记得修改Worker role 的app.config配置文件,去掉<Endpoint address>中地址的端口号,保持http://localhost/myMessageLogger.svc 的配置。

  之后可以编写Worker Role的代码,通过WCF Proxy往Web Role的文件存储中增加消息了。

1: public override void Start()  
2:    
3: {  
4:    
5: // This is a sample worker implementation. Replace with your logic.  
6:    
7: RoleManager.WriteToLog("Information", "Worker Process entry point called");  
8:    
9: string wcf_url = "http://localhost/myMessageLogger.svc";
10:    
11: while (true)  
12:    
13: {  
14:    
15: Thread.Sleep(10000);  
16:    
17: BasicHttpBinding bind = new BasicHttpBinding();  
18:    
19: EndpointAddress endpoint = new EndpointAddress(wcf_url);  
20:    
21: HostingWCFServices.MessageLoggerClient client = new HostingWCFServices_WorkerRole.HostingWCFServices.MessageLoggerClient(bind, endpoint);
22:    
23: client.LogMessage("Ping from WorkerRole");  
24:    
25: RoleManager.WriteToLog("Information", "Ping from WorkerRole-WCF MessageLoggerClient");  
26:    
27: if (client.State == CommunicationState.Faulted)  
28:    
29: client.Abort();  
30:    
31: else  
32:    
33: client.Close();  
34:    
35: }  
36:    
37: }  
38:  

 

  这样后台Worker Role的代码也完成了,然后F5 运行。运行界面如下:

 

  通过在Web Role中Hosting WCF Services可以让我们找到另外一个共享Azure能力或服务的途径,共享和暴露出来的WCF可以给Azure Services的其他角色使用,也可以提供给云端之外的应用和客户端使用,这也是Windows Azure平台提供应用组合的一个强有力的工具和途径。

  另外最后的体验中,我们也尝试了使用Worker Role调用和消费这个WCF服务,同样这也向我们指明了另外一个应用整合的途径,我们的Azure Services本身也可以消费来自其他Windows Azure平台或Internet上的服务和能力。而Azure Services的WebRole和Worker Role的体系架构也让看到云端应用的特色和优美之处。

0
相关文章