如何调试Windows服务?或者你知道它崩溃的原因吗?

时间:2016-11-28 04:23:38

标签: c# .net visual-studio windows-services

启动服务后的错误是

Application: BoardScanner.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.DivideByZeroException
   at BoardScanner.PageRetriever+<LoadAndSavePage>d__2.MoveNext()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_1(System.Object)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

然而,我似乎无法附加到进程并点击断点,因为它停止运行如此之快。

我的服务看起来像

public class PageRetriever : ServiceBase
{
    private PageDocumentRepository repo = new PageDocumentRepository();

    private SystemTimer PageLoadTimer = new SystemTimer() { Interval = 1000, Enabled = true };

    private static int secondsPerDay = TimeSpan.FromDays(1).Seconds;

    int tick = 0;

    private Queue<Task<PageRetrieval>> PageRetrievals = new Queue<Task<PageRetrieval>>();

    public PageRetriever()
    {
        ServiceName = "PageRetriever";
        CanStop = true;
        CanPauseAndContinue = true;
        AutoLog = true;
        PageLoadTimer.Elapsed += new ElapsedEventHandler(LoadAndSavePage);
    }

    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry(ServiceName + " started");
        base.OnStart(args);
        PageLoadTimer.Start();
    }

    protected override void OnStop()
    {
        base.OnStop();
        PageLoadTimer.Stop();
    }

    private async void LoadAndSavePage(object source, ElapsedEventArgs e)
    {
        // once every 60 seconds, start a first page scan
        if(tick % 60 == 0)
        {
            QueueUpPageScan(0);
        }

        // once every day, start a full page scan 
        if(tick % secondsPerDay == 0)
        {
            QueueUpPageScan(1);
        }

        // if there are no tasks to run, do nothing
        if(PageRetrievals.Count == 0)
        {
            return;
        }

        // run the dequeued task
        var retrieval = await PageRetrievals.Dequeue();

        // if we were able to get the html, add it to the db
        if(retrieval.Html != null)
        {
            await repo.Add(
                new PageDocument()
                {
                    Html = retrieval.Html.ToString(),
                    PageType = (short)retrieval.Page,
                    DateAdded = DateTime.Now,
                    DateProcessed = null
                }
            );

            // if this was part of a full page scan, queue up a task to get the next page
            if(retrieval.Page == PageType.Board && retrieval.Number > 0)
            {
                QueueUpPageScan(retrieval.Number + 1);
            }
        }

        // increment counter of how many times this function has been called
        tick += 1;
    }

    private void QueueUpPageScan(int pagenum)
    {
        PageRetrievals.Enqueue(new Task<PageRetrieval>(() =>
            new PageRetrieval()
            {
                Html = PageOpener.GetBoardPage(pagenum),
                Page = PageType.Board,
                Number = pagenum
            }
        ));
    }
}

它正在构建和安装。当我运行它时,我得到上述错误,我不确定如何调试它。有什么想法吗?

0 个答案:

没有答案