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

相关文章: