从Windows服务打开MVC4网页 [英] Open a MVC4 Web Page from Windows Service

查看:83
本文介绍了从Windows服务打开MVC4网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
            }

推荐答案

你的架构看起来很好....至少很奇怪。但是让我们跳过那一部分。



服务最有可能在一个系统用户下运行。那个没有配置文件,但即使您使用专用用户,该服务也在没有配置文件的情况下运行。所以像这样的启动过程是行不通的。此外,如果你有Vista或更高版本,它会在会话0中运行,其中启动交互式应用程序可能会导致意外的结果。

但是如果你想从服务发出http请求,你就不要我需要(实际上你永远不应该)使用浏览器这样做。为此,.net中有工具: WebClient [ ^ ]和 HttpWebrequest [ ^ ]。使用它们代替它们将起作用。您需要优化代码,但在Windows窗体或控制台中可以使用的代码也可以在服务中使用。但是如果需要身份验证,请注意正在运行的用户。
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.


这篇关于从Windows服务打开MVC4网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆