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 (之后找到不错的文章)

最新文章

  1. 一个技术汪的开源梦 —— 基于 .Net Core 的公共组件之序列化
  2. grape动态PHP结构(三)——API接口
  3. linux虚拟机上不了网--桥接方式--问题一直未解决
  4. JS之函数表达式
  5. Struts1 action重定向跳转 带参数
  6. FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
  7. WebForm水印照片
  8. mysql 联合索引(转)
  9. Qt之QLCDNumber
  10. postsharp初体验
  11. HDU3994(Folyd + 期望概率)
  12. SSD的优势
  13. AIX 中以并发模式挂载vg
  14. win下搭建python3+PyQt5+eric6环境
  15. nodejs轻量级时间格式化组件Moment.js的使用例子
  16. 管理外部表(External Tables)
  17. 使用InternalsVisibleTo给assembly添加“友元assembly”
  18. Doctrine2-基础概念
  19. Nginx 简单安装
  20. NOIP2018TG题解

热门文章

  1. wordpress get_query_var()函数
  2. Python基础学习三 字符串
  3. sklearn中的cross_val_score()函数
  4. REST的含义和RESTful架构入门
  5. 概览JVM的基本结构和JVM内存结构
  6. 详解PHP执行定时任务的实现思路
  7. Resin 的watchdog(看门狗)介绍和resin负载均衡实现
  8. IDEA MAVEN 1.8无效的源发行版
  9. linux tcpdump补充
  10. 367. Valid Perfect Square判断是不是完全平方数