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

posted on 2013-09-03 17:16  aking96  阅读(319)  评论(0编辑  收藏  举报