Communication with each role instance in Azure

本文指导如何在Azure中使用角色实例构建WCF服务和客户端,包括服务端实现和客户端调用流程。

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<IWcfChannel>(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:

 http://www.cnblogs.com/threestone/archive/2012/03/05/2379395.html (之后找到不错的文章)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值