Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
Database: MySQL MVC4 Web Application run on IIS Locally Windows Service

I have written a Windows Service to automatically Log data from few Machines and save it into Database. Its working fine, But i have a senario where one machine data will be logged and saved to database and after that i need to open a MVC4 webpage(specific url) from windows service by passing primary key of that table as parameter.

WebPage consists of 2 textboxes.

I am able to achieve this task in Windows forms with the same code but i unable to do it in Windows service.i am not getting any error data is getting logged and saving into database and but its not opening webpage.


Here is my Code:
try
            {
                int id = 0;
                String sql1c = "SELECT WeighID FROM weighing_scales_tbl WHERE MachineID = " + machid + " AND GroupWt = '" + weigh + "'  ORDER BY InsertedDate DESC";
                MySqlDataAdapter da1c = new MySqlDataAdapter(sql1c, mc.msqlConnection);
                DataTable dt1c = new DataTable();
                da1c.Fill(dt1c);
                if (dt1c.Rows.Count != 0)
                {
                    id = Convert.ToInt32(dt1c.Rows[0][0]);
                }

                StreamWriter str = new StreamWriter("D:\\Popup.txt", true);

                str.WriteLine("Popup Exception on : " + DateTime.Now.ToString() + "\n " );

                str.Close();
                 ProcessStartInfo startInfo = new ProcessStartInfo("chrome.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Minimized;

                //Process.Start(startInfo);

                StreamWriter str1 = new StreamWriter("D:\\Popup.txt", true);

                str1.WriteLine("Popup Starts on : " + DateTime.Now.ToString() + "\n ");

                str1.Close();

                startInfo.Arguments = "http://localhost:4113/LooseShot/AddLooseshot/" + id + "";

                StreamWriter str2 = new StreamWriter("D:\\Popup.txt", true);

                str2.WriteLine("Popup open on : " + DateTime.Now.ToString() + "\n ");

                str2.Close();

                Process.Start(startInfo);


                //System.Diagnostics.Process.Start("http://localhost:4113/LooseShot/AddLooseshot/" + id + "");
            }
            catch (SocketException se)
            {
                StreamWriter str = new StreamWriter("D:\\Popup.txt", true);

                str.WriteLine("Popup Exception on : " + DateTime.Now.ToString() + "\n " + se.Message);

                str.Close();
            }
Posted
Comments
Zoltán Zörgő 17-Jan-15 6:52am    
Any progress?
Sharath.krishna 20-Jan-15 23:56pm    
Hi Zoltan could you please explain how to do that using Webclient or Httpwebrequest.
i am unable to figure it out.
Zoltán Zörgő 21-Jan-15 5:38am    
I can try but you have to better explain the desired interaction between your service and the "remote" web application.

1 solution

Your architecture looks well.... at least strange. But let's skip that part.

A service is most likely running under one of the system users. That one has no profile, but even if you use a dedicated user, the service is running without profile. So starting processes like this won't work. Besides, if you have Vista or above, it runs in Session 0, in which starting interactive applications might result in unexpected outcome.
But if you want to issue a http request from a service, you don't need to (actually you never should) do this using a browser. For that there are tools in .net: WebClient[^] and HttpWebrequest[^]. Use those instead they will work. You will need to refine your code, but what will work in Windows Forms or as console, will work also in a service. But be aware of the running user if you need authentication.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900