Wrapping a C# service in a console app to debug it

前端 未结 10 1213
遇见更好的自我
遇见更好的自我 2020-12-15 03:36

I want to debug a service written in C# and the old fashioned way is just too long. I have to stop the service, start my application that uses the service in debug mode (Vis

10条回答
  •  轮回少年
    2020-12-15 04:31

    You can do something like this in the main entry point:

    static void Main()
    {
    #if DEBUG
        Service1 s = new Service1();
        s.Init(); // Init() is pretty much any code you would have in OnStart().
    #else
        ServiceBase[] ServicesToRun;
        ServicesToRun=new ServiceBase[] 
        { 
            new Service1() 
        };
        ServiceBase.Run(ServicesToRun);
    #endif
    }
    

    and in your OnStart Event handler:

    protected override void OnStart(string[] args)
    {
        Init();
    }
    

提交回复
热议问题