Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2010 如何调试Windows服务应用程序?_Visual Studio 2010_Debugging_Windows Services - Fatal编程技术网

Visual studio 2010 如何调试Windows服务应用程序?

Visual studio 2010 如何调试Windows服务应用程序?,visual-studio-2010,debugging,windows-services,Visual Studio 2010,Debugging,Windows Services,我正在Visual Studio 2010 Ultimate SP1中创建一个Windows服务应用程序 我在遵循MSDN中的“操作方法”: 我遇到了两个问题: 我无法通过服务器资源管理器启动服务-此处列出了我的服务,但在关联菜单中,我只有两个可用选项:刷新和属性。虽然MSDN文档说应该有这个选项,但没有“开始”。 幸运的是,通过使用服务控制管理器,我可以避免这种麻烦 下一步是:“在VisualStudio中,从调试菜单中选择进程”。 调试菜单中不存在该选项。我只有“附加到进程”,但服务没有列

我正在Visual Studio 2010 Ultimate SP1中创建一个Windows服务应用程序

我在遵循MSDN中的“操作方法”:

我遇到了两个问题:

  • 我无法通过服务器资源管理器启动服务-此处列出了我的服务,但在关联菜单中,我只有两个可用选项:刷新和属性。虽然MSDN文档说应该有这个选项,但没有“开始”。 幸运的是,通过使用服务控制管理器,我可以避免这种麻烦

  • 下一步是:“在VisualStudio中,从调试菜单中选择进程”。 调试菜单中不存在该选项。我只有“附加到进程”,但服务没有列在那里

  • 有人知道哪里出了问题,我应该如何调试我的应用程序吗


    提前谢谢。

    我想:我已经构建了很多Windows服务,出于许多原因之一,我没有在服务本身中创建核心代码。如果您愿意,该服务本质上是“操作层”。在dll中创建核心代码允许调试和测试该特定代码。您可以创建一个控制台或桌面应用程序,该应用程序将运行可在开发和测试阶段使用的核心代码

    就我个人而言,我创建了一个ServiceRunner应用程序,它结合启动和停止功能捕获日志记录。我的OnStart和OnStop代码块与服务的代码块完全相同

    接下来,当您测试服务时,您应该能够启动服务(例如myService.exe)并连接到进程。但是,另一个注意事项是,您应该暂停/等待带有调试构建的服务线程(比如30秒),以便有时间连接到进程,并且不会错过初始化代码。请记住,您必须先安装服务,然后通过Windows服务管理器启动

    下面是一些代码,可以为您指明我使用的方向。在service program.cs文件中,我使用以下命令:;然后在serviceonstart()方法中调用dll并运行。此外,您可以停止服务,用更新的版本替换dll,然后重新启动。使用C++,你也可以替换服务exe,但是这些只是C的特性:C++中,你不能。
    static class Program
    {
        public const string SERVICE_NAME = "myService";
        public const string SERVICE_DISPLAY_NAME = "My Service";
    
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            if (args != null && args.Length > 0)
            {
                foreach (string arg in args)
                {
                    switch (arg.ToLower())
                    {
                        case "-install":
                            ManageService(true);
                            return;
    
                        case "-remove":
                            ManageService(false);
                            return;
                    }
                }
    
    
            }
    
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
                    { 
                        new Service() 
                    };
            ServiceBase.Run(ServicesToRun);
        }
    
        private static void ManageService(bool bInstall)
        {
            string parms;
    
            if (bInstall == true)
            {
                parms = string.Format("Create {0} type= own start= demand binPath= \"{1}\" DisplayName= \"{2}\"", SERVICE_NAME,
                                      System.Reflection.Assembly.GetExecutingAssembly().Location, SERVICE_DISPLAY_NAME);
            }
            else // remove
            {
                parms = string.Format("Delete {0}", SERVICE_NAME);
            }
    
            try
            {
                string output = string.Empty;
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("sc.exe", parms);
    
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.CreateNoWindow = true;
    
                proc.StartInfo = startInfo;
    
                proc.Start();
    
                output = proc.StandardOutput.ReadToEnd();
    
                proc.WaitForExit(10000);
    
                if (proc.HasExited == true)
                {
                    // NOTE: The project type has been changed from Windows Service to Console Application
                    // so that Console.WriteLine will output to the console
                    Console.WriteLine(output);
                }
                else
                {
                    proc.Close();
                    Console.WriteLine("Timed out waiting to install service");
                }
            }
            catch (System.ComponentModel.Win32Exception)
            {
                Console.WriteLine("Unable to locate sc.exe");
            }
        }
    }
    
    静态类程序
    {
    public const string SERVICE_NAME=“myService”;
    公用常量字符串服务\u显示\u NAME=“我的服务”;
    /// 
    ///应用程序的主要入口点。
    /// 
    静态void Main(字符串[]参数)
    {
    如果(args!=null&&args.Length>0)
    {
    foreach(args中的字符串arg)
    {
    开关(arg.ToLower())
    {
    外壳“-安装”:
    管理服务(真实);
    返回;
    案例“-移除”:
    管理服务(假);
    返回;
    }
    }
    }
    ServiceBase[]ServicesToRun;
    ServicesToRun=新的ServiceBase[]
    { 
    新服务()
    };
    ServiceBase.Run(ServicesToRun);
    }
    专用静态void管理服务(bool-bInstall)
    {
    弦杆;
    如果(bInstall==true)
    {
    parms=string.Format(“创建{0}type=own start=demand binPath=\“{1}\”显示名称=\“{2}\”,服务名称,
    System.Reflection.Assembly.getExecutionGassembly().Location,SERVICE\u DISPLAY\u NAME);
    }
    else//删除
    {
    parms=string.Format(“删除{0}”,服务名称);
    }
    尝试
    {
    字符串输出=string.Empty;
    System.Diagnostics.Process proc=新的System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo=新的System.Diagnostics.ProcessStartInfo(“sc.exe”,parms);
    startInfo.UseShellExecute=false;
    startInfo.RedirectStandardOutput=true;
    startInfo.RedirectStandardError=true;
    startInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.CreateNoWindow=true;
    proc.StartInfo=StartInfo;
    proc.Start();
    output=proc.StandardOutput.ReadToEnd();
    WaitForExit程序(10000);
    如果(proc.HasExited==true)
    {
    //注意:项目类型已从Windows服务更改为控制台应用程序
    //这样Console.WriteLine将输出到控制台
    控制台写入线(输出);
    }
    其他的
    {
    过程关闭();
    Console.WriteLine(“等待安装服务超时”);
    }
    }
    捕获(System.ComponentModel.Win32Exception)
    {
    Console.WriteLine(“无法找到sc.exe”);
    }
    }
    }
    
    //From the main function a method from service class can be called like below code
    
    
    //DebugApp method can be called from main and so the service can be debug:
    //Service class    
    public partial class CServices : ServiceBase
    {
        public CServices()
        {
            InitializeComponent();
        }
    
        **public void DebugApp()
        {
            OnStart(new string[]{});
        }**
        protected override void OnStart(string[] args)
        {
            System.Console.WriteLine("Testing");
            System.Console.Read();
        }
    
        protected override void OnStop()
        {
        }
    }
    
    
    //Calling from main: 
    
       static void Main()
        {
            Services1.CServices uc = new CServices();
            uc.DebugApp();
        }