我正在分析我(退休的)同事的windows应用程序源(C#)
当我单击“应用程序中的服务开始”按钮时,该服务打开“启动”,但2-3秒后停止。因此,我签入了事件查看器,它有一些问题。
进程终止于
System.Data.SqlClient.SqlException。
所以我试着去找原因,但我不知道该怎么做。
首先,我尝试在Visual中使用进程调试器,
但我之前提到过,进程在2-3秒内就停止了,所以,这是不可能的.
如何检查错误或调试服务?
我有一个完整的线人。谁来帮帮我。
发布于 2017-02-15 08:15:50
让你喜欢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中的模式开关"Debug/Release“,您的Program.cs文件将被启用/禁用。如果它在调试中,那么调试部分将被启用,其他部分将被注释/禁用,反之亦然。
发布于 2017-02-15 08:26:23
可以使用下面的代码调试web服务代码。
静态类程序{
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);
}
}
https://stackoverflow.com/questions/42243262
复制相似问题