Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

c# - windows service won't execute


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately without you giving us an exception or stack trace we havent got much to go on here.

So, first of all, you need to find out which line of code fails when running as a "windows service" and not when you run the code "interactively".

One thing you could do is wrap your function with a try/catch clause and catch the exception and log it to a file.

In addition to that, another option would be for you to debug the code whilst it is running as a windows service and then put a breakpoint on the function in question and step through the code.

To do that, temporarily add the following code to the service:

static void Main(string[] args)
{
    if (Debugger.IsAttached == false)
        Debugger.Launch();
    ...

Then, make sure you have visual studio open with your solution loaded. Then, when you start your service, it will pop up a dialog and ask for you to either attach to an "existing instance" of visual studio or to "open a new instance". Select the option to attach to an "existing instance" with your code open, and then you will be able to step through the code as normal, set breakpoints, etc, only this time the code is running under the credentials of the service.

From here, you should be able to track down the code that fails while running as a service.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...