我正在分析我的(已退休的)同事的Windows应用程序源代码(C#)
当我在应用程序中单击服务启动按钮时,该服务开始启动,但2-3秒后它停止了。所以,我检查了登录事件查看器,它有一些问题。
该过程由
终止System.Data.SqlClient.SqlException。
所以我试着找到原因,但我不知道怎么能这样做。
首先,我尝试在Visual Studio中使用Process debugger,
但是我之前提到,这个过程仅在2-3秒内就停止了,所以,这是不可能的......
如何检查错误或调试服务???
我有一个完整的来源。请有人帮助我。
答案 0 :(得分:0)
让你像Program.cs一样
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Service1 myService = new Service1();
myService.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
}
并且您的Service1.cs文件应该像..
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
// your code to do something
}
protected override void OnStop()
{
}
现在,基于Visual Studio中的“调试/释放”模式,您的Program.cs文件将被启用/禁用。如果它在调试中,那么将启用调试部分,其他将被注释/禁用,反之亦然。
答案 1 :(得分:0)
您可以使用以下代码调试您的网络服务代码。
静态类程序 {
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new DataTransfer()
};
if (Environment.UserInteractive)
{
RunInteractive(ServicesToRun);
}
else
{
ServiceBase.Run(ServicesToRun);
}
//ServiceBase.Run(ServicesToRun);
}
static void RunInteractive(ServiceBase[] servicesToRun)
{
Console.WriteLine("Services running in interactive mode.");
Console.WriteLine();
MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart",
BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ServiceBase service in servicesToRun)
{
Console.Write("Starting {0}...", service.ServiceName);
onStartMethod.Invoke(service, new object[] { new string[] { } });
Console.Write("Started");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(
"Press any key to stop the services and end the process...");
Console.ReadKey();
Console.WriteLine();
MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop",
BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ServiceBase service in servicesToRun)
{
Console.Write("Stopping {0}...", service.ServiceName);
onStopMethod.Invoke(service, null);
Console.WriteLine("Stopped");
}
Console.WriteLine("All services stopped.");
// Keep the console alive for a second to allow the user to see the message.
Thread.Sleep(1000);
}
}