A windows service that calls a wcf service every minute.

June 5, 2013 at 08:24:30
Specs: Windows 7
I have a windows service that calls a wcf service every minute, but it seems to only call it once and then never loops back to call again. Here is my windows service code.

namespace testWindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public static Service1Client client = new Service1Client("BasicHttpBinding_IService1");

        public Service1()
        {
            InitializeComponent();

        }

        protected override void OnStart(string[] args)
        {

                backgroundWorker1.RunWorkerAsync();

        }

        protected override void OnStop()
        {

            backgroundWorker1.Dispose();
            this.Stop();
            client.Close();
            
        }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                client.Open();
                client.calcLat();
                Thread.Sleep(60000);
                client.Close();
            }

And then my wcf service.

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Service1 : IService1
    {
        public static double infinity = double.PositiveInfinity;
        public static int pingCount = 0;
        public static long avgRtt;
  

        #region IService1 Members

        public void submitData(Latency latency)
        {
            SqlConnection cs = new SqlConnection(@"Server=130-Adl-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

            cs.Open();

            SqlCommand insert = new SqlCommand(@"Insert into NIM_Latency (stationName, latency, stationIP, availability) values ('" + latency.stationName + "', '" + latency.latency + "', '" + latency.stationip + "', 'reachable')", cs);
            insert.CommandType = CommandType.Text;
            insert.ExecuteNonQuery();
            cs.Close();


        }

        public void updateData(Latency latency)
        {
            SqlConnection cs = new SqlConnection(@"Server=130-Adl-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

            cs.Open();

            SqlCommand update = new SqlCommand(@"Update NIM_Latency set latency = '" + latency.latency + "', availabilty = 'reachable' where stationName = '" + System.Environment.MachineName + "'", cs);
            update.CommandType = CommandType.Text;
            update.ExecuteNonQuery();
            cs.Close();

        }

        public void unreachableUpdate(Latency latency)
        {
            SqlConnection cs = new SqlConnection(@"Server=130-Adl-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

            cs.Open();

            SqlCommand update = new SqlCommand(@"Update NIM_Latency set latency = '" + latency.latency + "', availabilty = 'unreachable' where stationName = '" + System.Environment.MachineName + "'", cs);
            update.CommandType = CommandType.Text;
            update.ExecuteNonQuery();
            cs.Close();
        }

        public void unreachableSubmit(Latency latency)
        {
            SqlConnection cs = new SqlConnection(@"Server=130-Adl-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

            cs.Open();

            SqlCommand insert = new SqlCommand(@"Insert into NIM_Latency (stationName, latency, stationIP, availability) values ('" + latency.stationName + "', '" + latency.latency + "', '" + latency.stationip + "', 'unreachable')", cs);
            insert.CommandType = CommandType.Text;
            insert.ExecuteNonQuery();
            cs.Close();

        }



        public void calcLat()
        {

  
                    var ping = new Ping();

                    var reply = ping.Send("10.209.224.100", 3000);

                    avgRtt = (avgRtt * pingCount++ + reply.RoundtripTime) / pingCount;


                    pingCount++;


                    if (reply.Status != IPStatus.Success)
                    {
                        SqlConnection cs = new SqlConnection(@"Server=130-Adl-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

                        SqlCommand cmd = new SqlCommand("select * from NIM_Latency where stationName='" + System.Environment.MachineName + "'", cs);

                        cmd.CommandType = CommandType.Text;

                        cmd.Connection = cs;

                        cs.Open();

                        SqlDataReader dr = cmd.ExecuteReader();


                        dr.Read();
                        if (dr.HasRows)
                        {
                            IPHostEntry host;
                            string localIP = "";
                            host = Dns.GetHostEntry(Dns.GetHostName());
                            foreach (IPAddress ip in host.AddressList)
                            {
                                if (ip.AddressFamily.ToString() == "InterNetwork")
                                {
                                    localIP = ip.ToString();
                                }
                            }

                            Latency lat = new Latency();
                            lat.stationName = System.Environment.MachineName;
                            lat.latency = avgRtt;
                            lat.stationip = localIP;
                            unreachableUpdate(lat);
                        }
                        else
                        {
                            IPHostEntry host;
                            string localIP = "";
                            host = Dns.GetHostEntry(Dns.GetHostName());
                            foreach (IPAddress ip in host.AddressList)
                            {
                                if (ip.AddressFamily.ToString() == "InterNetwork")
                                {
                                    localIP = ip.ToString();
                                }
                            }

                            Latency lat = new Latency();
                            lat.stationName = System.Environment.MachineName;
                            lat.latency = avgRtt;
                            lat.stationip = localIP;
                            unreachableUpdate(lat);
                        }
                    }



                    if (pingCount == 4)
                    {


                        SqlConnection cs = new SqlConnection(@"Server=130-Adl-P-001.OSCA.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

                        SqlCommand cmd = new SqlCommand("select * from NIM_Latency where stationName='" + System.Environment.MachineName + "'", cs);

                        cmd.CommandType = CommandType.Text;

                        cmd.Connection = cs;

                        cs.Open();

                        SqlDataReader dr = cmd.ExecuteReader();


                        dr.Read();
                        if (dr.HasRows)
                        {


                            IPHostEntry host;
                            string localIP = "";
                            host = Dns.GetHostEntry(Dns.GetHostName());
                            foreach (IPAddress ip in host.AddressList)
                            {
                                if (ip.AddressFamily.ToString() == "InterNetwork")
                                {
                                    localIP = ip.ToString();
                                }
                            }

                            Latency lat = new Latency();
                            lat.stationName = System.Environment.MachineName;
                            lat.latency = avgRtt;
                            lat.stationip = localIP;

                            updateData(lat);

                            pingCount = 0;
                            


                        }
                        else
                        {

                            IPHostEntry host;
                            string localIP = "";
                            host = Dns.GetHostEntry(Dns.GetHostName());
                            foreach (IPAddress ip in host.AddressList)
                            {
                                if (ip.AddressFamily.ToString() == "InterNetwork")
                                {
                                    localIP = ip.ToString();
                                }
                            }

                            Latency lat = new Latency();
                            lat.stationName = System.Environment.MachineName;
                            lat.latency = avgRtt;
                            lat.stationip = localIP;

                            submitData(lat);

                            pingCount = 0;
                        

                            cs.Close();

                        }
                    }

                }
               
            

            }

        }


        #endregion
    


See More: A windows service that calls a wcf service every minute.

Report •

#1
June 5, 2013 at 14:24:51
Just to say that this is what WCF is:
http://msdn.microsoft.com/en-us/lib...

Wait for help with coding.

Always pop back and let us know the outcome - thanks


Report •
Related Solutions


Ask Question