Communication with each role instance in Azure
2024-09-05 02:19:32
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 (之后找到不错的文章)
最新文章
- 一个技术汪的开源梦 —— 基于 .Net Core 的公共组件之序列化
- grape动态PHP结构(三)——API接口
- linux虚拟机上不了网--桥接方式--问题一直未解决
- JS之函数表达式
- Struts1 action重定向跳转 带参数
- FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
- WebForm水印照片
- mysql 联合索引(转)
- Qt之QLCDNumber
- postsharp初体验
- HDU3994(Folyd + 期望概率)
- SSD的优势
- AIX 中以并发模式挂载vg
- win下搭建python3+PyQt5+eric6环境
- nodejs轻量级时间格式化组件Moment.js的使用例子
- 管理外部表(External Tables)
- 使用InternalsVisibleTo给assembly添加“友元assembly”
- Doctrine2-基础概念
- Nginx 简单安装
- NOIP2018TG题解
热门文章
- wordpress get_query_var()函数
- Python基础学习三 字符串
- sklearn中的cross_val_score()函数
- REST的含义和RESTful架构入门
- 概览JVM的基本结构和JVM内存结构
- 详解PHP执行定时任务的实现思路
- Resin 的watchdog(看门狗)介绍和resin负载均衡实现
- IDEA MAVEN 1.8无效的源发行版
- linux tcpdump补充
- 367. Valid Perfect Square判断是不是完全平方数