简体   繁体   English

如何在Visual Studio 2010中调试Window服务

[英]how to debug Window service in visual studio 2010

I have create window service that read data from database.But, It does not read data from database.I am trying to debug but could not happen. 我创建了从数据库读取数据的窗口服务。但是,它不从数据库读取数据。我正在尝试调试,但无法发生。 I have written several log using eventviewer.but Log are not written in eventviewer.However, all the code work in Window form applications, the code is 我已经使用eventviewer编写了多个日志,但是Log并不是在eventviewer中编写的。但是,所有代码都可以在Window窗体应用程序中使用,代码是

eventLog1.WriteEntry("GApps Sync is Collecting parameters from GUI seting", System.Diagnostics.EventLogEntryType.Information);
       var folderPath = Path.Combine(Environment.GetFolderPath(
         Environment.SpecialFolder.ApplicationData), "GAppsSync");
       if (!Directory.Exists(folderPath))
       {
           eventLog1.WriteEntry("Please stop service and Run GUI Tool set all the syn parameter", System.Diagnostics.EventLogEntryType.Error);          
       }
        path = Path.Combine(folderPath, "databaseFile.db3");
        FileInfo fileInfo = new FileInfo(path);
        if (fileInfo.Exists)
        {
            eventLog1.WriteEntry("GApps Sync is getting parameters from GUI tool", System.Diagnostics.EventLogEntryType.Information);
            try
            {
                using (SQLiteConnection con = new SQLiteConnection("data source=" + path))
                {
                    con.Open();
                    SQLiteCommand cmdSyncPara = new SQLiteCommand("Select SyncInterval, CRMSetting,GoogleSetting,SyncOption From Synchroniszation",con);
                    SQLiteDataReader dataReader = cmdSyncPara.ExecuteReader();
                    eventLog1.WriteEntry("GApps Sync is Reading database parameter:", System.Diagnostics.EventLogEntryType.Error);
                    while (dataReader.Read())
                    {
                        SyncInterval = dataReader.GetString(0);
                        eventLog1.WriteEntry("GApps Sync is getting parameters from GUI tool Syncinterval:" + SyncInterval, System.Diagnostics.EventLogEntryType.Information);
                        CRMSetting = dataReader.GetString(1);
                        eventLog1.WriteEntry("GApps Sync is getting parameters from GUI tool CRMSetting:" + CRMSetting, System.Diagnostics.EventLogEntryType.Information);

                        GoogleSetting = dataReader.GetString(2);
                        SyncOption = dataReader.GetString(3);
                    }
                    eventLog1.WriteEntry("GApps Sync got GUI sync Options and Sync Interval parameters", System.Diagnostics.EventLogEntryType.Information); 
                    SQLiteCommand cmdReadData = new SQLiteCommand("Select Enable, GmailId,GmailPassword,EmployeeAccount From SyncDataDetail",con);
                    SQLiteDataReader dataReaderDetail = cmdReadData.ExecuteReader();
                    while (dataReaderDetail.Read())
                    {
                        DataContainer dc = new DataContainer();
                        dc.Enable = bool.Parse(dataReaderDetail.GetString(0));
                        dc.EmailText = dataReaderDetail.GetString(1);
                        dc.Password = Decrypt(dataReaderDetail.GetString(2));
                        dc.EmployeeAccount = dataReaderDetail.GetString(3);
                        ItemCollection.Add(dc);
                    }
                    eventLog1.WriteEntry("GApps Sync got GUI Save Account Mapping ", System.Diagnostics.EventLogEntryType.Information); 
                }
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("GApps Sync Failed to get GUI Save Account Mapping ", System.Diagnostics.EventLogEntryType.Error); 
            }
        }            
    }

You should attach the process to the debugger in visual studio. 您应该将流程附加到Visual Studio中的调试器。 You can do this from the debug menu. 您可以从调试菜单中执行此操作。 Then clicking on attach to process. 然后单击附加到进程。 A new window displays, select the process there and click on attach. 将显示一个新窗口,在此处选择过程,然后单击附加。 Now you are in debug mode for your windows service. 现在,您处于Windows服务的调试模式。

Source: http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx 来源: http : //msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx

the best way to debug the windows services edit your program.cs file like this 调试Windows服务的最佳方法是像这样编辑program.cs文件

static class Program { 
    static void Main()  { 

        #if DEBUG ServiceName myService = new ServiceName();
            myService.onDebug(); 
        #else ServiceBase[] ServicesToRun;

            ServicesToRun = new ServiceBase[] 
            {  
                new ServiceName() 
            }; 

            ServiceBase.Run(ServicesToRun); 

        #endif
    } 
}

then run the file in debug mode add the break point where you want that. 然后在调试模式下运行文件,在所需位置添加断点。 :) :)

From MSDN ( Debug Windows Service ) 从MSDN( 调试Windows服务

Add a method to your service that runs the OnStart and OnStop methods: 将一种方法添加到运行OnStart和OnStop方法的服务中:

internal void TestStartupAndStop(string[] args)
{
    this.OnStart(args);
    Console.ReadLine();
    this.OnStop();
}

Rewrite the Main method as follows: 重写Main方法,如下所示:

static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                MyNewService service1 = new MyNewService(args);
                service1.TestStartupAndStop(args);
            }
            else
            {
                // Put the body of your old Main method here.
            }
       }

Note: You also need another constructor for your service ie: 注意:您还需要另一个服务构造函数,即:

public Service1(string[] args)

if you want to pass arguments. 如果您想传递参数。

In the Application tab of the project's properties, set the Output type to Console Application. 在项目属性的“应用程序”选项卡中,将“输出类型”设置为“控制台应用程序”。 (do not forget this step!) (不要忘记这一步!)

Choose Start Debugging (F5). 选择开始调试(F5)。

This will pop up a console window, run onStart, and patiently wait for you to hit a key in the console window to end your service. 这将弹出一个控制台窗口,在onStart上运行,并耐心地等待您在控制台窗口中击键以结束服务。

To run the program as a Windows Service again, install it and start it as usual for a Windows Service. 要再次将程序作为Windows服务运行,请安装该程序并像往常一样为Windows服务启动它。 It's not necessary to reverse these changes. 不必撤销这些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM