博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Communication with each role instance in Azure
阅读量:5875 次
发布时间:2019-06-19

本文共 3844 字,大约阅读时间需要 12 分钟。

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(10000);                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 = 12;            CreateServiceHost();            // For information on handling configuration changes            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.            return base.OnStart();        }
View Code

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[0].InstanceEndpoints["WcfEndpoint"].IPEndpoint.Address.ToString();            int tcpport = RoleEnvironment.Roles["WorkerRole1"].Instances[0].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
(binding, new EndpointAddress(serviceUrl)); IWcfChannel sss = ChannelFactory.CreateChannel(); string oldString = TextBox1.Text; if (!string.IsNullOrEmpty(oldString.ToString())) { Label1.Text = sss.ReverseString(oldString); } }
View Code

PS: If we need to Use role communication we need to add new endpoint and set type as Internal as following screenshot:

  (之后找到不错的文章)

转载于:https://www.cnblogs.com/akingyao/p/3299117.html

你可能感兴趣的文章
Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)...
查看>>
在odl中怎样实现rpc
查看>>
leetcode 110 Balanced Binary Tree
查看>>
python活用isdigit方法显示系统进程
查看>>
项目开发总结
查看>>
知行合一
查看>>
jmeter插件之jsonpath提取响应结果和做断言
查看>>
发布支持多线程的PowerShell模块 —— MultiThreadTaskRunner
查看>>
Ubuntu ctrl+alt会导致窗口还原的问题
查看>>
第四十期百度技术沙龙笔记整理
查看>>
推荐系统那点事 —— 基于Spark MLlib的特征选择
查看>>
linux 下RTL8723/RTL8188调试记录(命令行)【转】
查看>>
開始新的征程
查看>>
SpringMVC案例1——对User表进行CRUD操作
查看>>
看雪CTF第十四题
查看>>
模拟生命_吸烟致癌?
查看>>
[Contiki系列论文之1]Contiki——为微传感器网络而生的轻量级的、灵活的操作系统...
查看>>
Android 网络编程 记录
查看>>
微软同步发行Windows 10和Windows 10 Mobile系统更新
查看>>
Maven 传递依赖冲突解决(了解)
查看>>