Communication with each role instance in Azure
Use WCF Communication with role instance in azure
1)In worker role build WCF Service
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.TraceInformation("WorkerRole1 entry point called", "Information"); while (true)
{
Thread.Sleep();
Trace.TraceInformation("Working", "Information");
}
}
private ServiceHost serviceHost; private void CreateServiceHost()
{
serviceHost = new ServiceHost(typeof(WcfService)); NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
// RoleInstanceEndpoint externalEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WcfEndpoint"]; string ip = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WcfEndpoint"].IPEndpoint.Address.ToString();
int tcpport = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WcfEndpoint"].IPEndpoint.Port; string endpoint = String.Format("net.tcp://{0}:{1}/LoanCalculator", ip, tcpport); //#region insert Instance Information to azure table
//InstanceInformation instance = new InstanceInformation();
//instance.IP = endpoint;
//instance.Port = tcpport;
//// cpu
//System.Diagnostics.PerformanceCounter PC = new System.Diagnostics.PerformanceCounter();
//// instance.CPURate=PC.
//#endregion
serviceHost.AddServiceEndpoint(typeof(IWcfService), binding, endpoint);
try
{
// ServiceLib.AzureTableMethod.InSertInstanceInformation("wcfTable", instance);
serviceHost.Open(); }
catch
{ }
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
// ServicePointManager.DefaultConnectionLimit = 12;
ServicePointManager.DefaultConnectionLimit = ;
CreateServiceHost();
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. return base.OnStart();
}
2)In Web role build WCF Client
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
// http://jambor.cloudapp.net/
// string serviceUrl = "net.tcp://jambor.cloudapp.net/LoanCalculator";
// string serviceUrl = String.Format("net.tcp://{0}:{1}/LoanCalculator", "127.0.0.1", "8082");
//string serviceUrl = "net.tcp://207.46.129.69:8082/LoanCalculator";
string address = RoleEnvironment.Roles["WorkerRole1"].Instances[].InstanceEndpoints["WcfEndpoint"].IPEndpoint.Address.ToString();
int tcpport = RoleEnvironment.Roles["WorkerRole1"].Instances[].InstanceEndpoints["WcfEndpoint"].IPEndpoint.Port;
// string d= TextBox2.Text;
string serviceUrl = string.Format("net.tcp://{0}:{1}/LoanCalculator", address, tcpport);
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None); ChannelFactory< IWcfChannel> ChannelFactory = new ChannelFactory<IWcfChannel>(binding, new EndpointAddress(serviceUrl));
IWcfChannel sss = ChannelFactory.CreateChannel();
string oldString = TextBox1.Text;
if (!string.IsNullOrEmpty(oldString.ToString()))
{
Label1.Text = sss.ReverseString(oldString);
}
}
PS: If we need to Use role communication we need to add new endpoint and set type as Internal as following screenshot:

http://www.cnblogs.com/threestone/archive/2012/03/05/2379395.html (之后找到不错的文章)
Communication with each role instance in Azure的更多相关文章
- Windows Azure Cloud Service (10) Role的生命周期
<Windows Azure Platform 系列文章目录> 在上一章内容中,我们提到了Windows Azure会依次调用角色(Role)实例的OnStart()方法和Run()方法. ...
- Windows Azure Cloud Service (11) PaaS之Web Role, Worker Role(上)
<Windows Azure Platform 系列文章目录> 本文是对Windows Azure Platform (六) Windows Azure应用程序运行环境内容的补充. 我们知 ...
- Windows Azure Cloud Service (12) PaaS之Web Role, Worker Role, Azure Storage Queue(下)
<Windows Azure Platform 系列文章目录> 本章DEMO部分源代码,请在这里下载. 在上一章中,笔者介绍了我们可以使用Azure PaaS的Web Role和Worke ...
- Windows Azure Cloud Service (42) 使用Azure In-Role Cache缓存(1)Co-located Role
<Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...
- Windows Azure Cloud Service (43) 使用Azure In-Role Cache缓存(2)Dedicated Role
<Windows Azure Platform 系列文章目录> Update 2016-01-12 https://azure.microsoft.com/zh-cn/documentat ...
- [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.
Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...
- [Windows Azure] Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5.
Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5. T ...
- [Windows Azure] Building the web role for the Windows Azure Email Service application - 3 of 5
Building the web role for the Windows Azure Email Service application - 3 of 5. This is the third tu ...
- [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5
.NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...
随机推荐
- JavaScript语言基础-基本数据类型与对象类型
- ResponseUtil
package util; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.http.Htt ...
- telnet测试端口是否正常打开
点击计算机的开始菜单-->运行 ,输入CMD命令,然后确定.打开cmd命令行. 输入telnet测试端口命令: telnet IP 端口 或者 telnet 域名 端口 回车 如果端口关 ...
- Linux下安装配置MySQL5.7服务器
Linux下安装配置MySQL服务器 一.安装环境 ============ OS:centos6.8 MySQL:mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz ...
- schedule和scheduleAtFixedRate的区别
- 刷题向》DP》值得一做》关于对DP问题的充分考虑(normal)
在你辛苦调试一道DP题,遇到瓶颈的时候,你是否感到一股洪荒之力遏制住你的思想,使你给题库贡献了一倍的WA.RE.TLE量,却没有AC过一次? 在这时,你应该考虑的是砸电脑再次重新考虑整个题目,再应对自 ...
- Eclipse导入tomcat服务器
创建server
- Asp.net MVC获取访问系统的客户端计算机的主机名和IP地址
string HostName = string.Empty; string ip = string.Empty; string ipv4 = String.Empty; if (!string.Is ...
- hdu4643 GSM
#include<stdio.h> #include<math.h> #define Max 55 #define eps 1e-8 int n,m; struct Point ...
- linux内存监控 free
free 命令详解: 执行命令后总共四行. 第一行: 列头 第二行: total 内存总数: 32881776 used 已经使用的内存数: 8324796 free 空闲的内存数: 24556980 ...