Run console program as service




















LogMessage "CtrlC pressed. WriteLine "CtrlC pressed. This is the installer for the service. Collections; using System. Install; using System. How I created this code It looks like a lot of code, but its all based on the Windows Service project template in Visual Studio Here are the extra References required these are automatically added when you create a new Windows Service using the template : System.

ServiceProcess System. Install You'll notice references to Gurock. Community Bot 1 1 1 silver badge. Contango Contango WriteLine this. Press any key to stop" ; Trace. WriteLine "" ; Console. ToUpperInvariant : String. WriteLine ex. ExceptionObject is Exception Trace. WriteLine Exception e. I have just tested this in Visual Studio and it works.

Create new "Windows Service" project. Write "Hit any key to continue If this works without bad side effects, it is nice short and clean. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back The command requires you to remove the slash. From this point on, your new service will run whenever Windows starts. Use Google Fonts in Word. Customize the Taskbar in Windows What Is svchost.

Best Home Theater Systems. Best Smartwatches. Best Gaming Laptops. Best Smart Displays. Best Home Security Systems. Best External Solid State Drives. Best Portable Chargers. Best Phone Chargers. Best Wi-Fi Range Extenders. Best Oculus Quest 2 Accessories.

Awesome PC Accessories. Best Linux Laptops. I see the Program. CS designer is showing up like this. I did forget something in the sample I posted for you. I should have included the following to lines in the MyService class:. Sorry about that The reason for the error you're seeing is because you probably have no threading or other mechanism keeping it "alive". Since you do it all with a timer. You'll notice that in my blog post, I use a thread to put my processing in.

Perhaps I should have mentioned that as well in my previous reply. In my blog post, look at the OnStart , OnStop and DoWork methods ignore the thing about SqlServer and just always start the service inside a while loop.

I would show you an example more tailored to your code, but in this case I think pointing it out to you in my blog post might be OK plus I wanted to get this reply to you posted quicker. And, I see that you've written two more posts since I was in the process of writing this one. I will read those and comment in another post. I'm not so sure now if your problem with the Service stopping itself has something to do with threading or not my blog post probably isn't going to help much for this, so let's ignore that for now.

If the Service keeps running fine, then there's a file access issue with where you're trying to write the file. If the Service still stops by itself, then we still don't know what the problem is and we'll have to dig some more probably use threading instead of the timer, but let's not rush into that one yet. In the code you posted, I don't see where you run this as a Console application, only where you run it as a Windows Service.

So, I'm not sure how you're testing this from VS. You should have the code in your Main method that I originally posted:. ReadLine , then stops the service. In other words, your console window will stay up until you hit any key Change it to this:. I can do that, but it won't be for awhile. I'm in the middle of doing some work right now and the boss is cracking the whip!

Actually, the boss is my husband and he doesn't usually get on my case for doing stuff other than work, as long as I don't spend all day not working! I might find some time later today, but I can't guarantee it If not today, then definitely over the weekend Apparently your web service proxy has a class called Thread, and the compiler is getting confused. Just fully-qualify the type name wherever used.

Thread threadStart ; myThread. Start ;. David, thanks for the help. I used your code and errors were gone. I ran the program in VS, got console window and message "service started" and the log file also was created "webserviceLog. This means that the DoWord is triggering. But when I install the windows service, I got this error message. You may need to put a pause in your service startup logic to give you time to attach the debugger. Read this Introduction to Windows Service Applications.

Yes, that;s the error I am talking about. In the debugging in VS for service. What I am looking to attach in the list if my service is not even starting? Yes, the error says it started but then it says it stopped so how debugging will happen when I start the service and it stops within a second?

Sorry just have much experience with debugging a service and I have tried so many things now to convert my console app to windows service, I am so much confused :. I just finished recreating your app from the code you've posted and installing as a Windows Service. It works fine, writing to the Log. Of course, I don't have that web service that you're using, so that part is commented out. Could that still be what's causing the problem, do you think?

Comment it out for now and see if it works. There may be another possible difference between my installation and yours, since I don't know how you did the installation. I always use a. Bonnie, I just sent you a 2nd email with an update that when I comment out the web service lines, the service installs and run fine by creating log file in the defined location.

I disagree. The issue is that you can't troubleshoot the service. You have no idea why it's failing. You have to implement the logging and debugging procedures that will enable you to identify and diagnose failures. A Windows service runs without a user interface, and is expected to run for months at a time. At a minimum you need to have logging that shows you everything that happened and any exception that occurs.

Once you have that, working through the problems is straightforward. Hello David, I completely understand what you are saying. I even checked windows event but since service start and stops right away, there is no event.

Could you please help me with code or guide me with the process of building the logging system so that I am troubleshoot the service and find out the cause of the problem? I understand this takes to the point if I have config correctly setup or not. I have double check my App. I don't know what to do next as I have my App. It's funny, I woke up in the middle of the night thinking that you probably had a config issue.

You might need to share your config here. Everything must go in the EXE project's app. When your Web Reference gets generated, one of the things it creates is the web settings in Properties, Settings. Those need to be put into your app. So, here's an example of the extra stuff that needed to go into my config:. In the above part of the app. Sorry about that! Ok Bonnie, I did the exact same thing as mention above. I used the same console application project and updated the code discussed here in Program.

CS and added the installer etc. After saving, this stuff got updated automatically in the app. So, we both have the section named "OurApp. Settings", so that part looks the same. Now, look at my setting name. Yours is simply "Setting". I think that's the problem. I don't know what name should come in so I tried the name similar to "contract" and "name" in binding settings.

One more try, Jeff. I think your setting still doesn't look right. I think it needs to be like this:. I sent you an email too. You might want to think about starting over with adding the web reference if the above suggestion doesn't work You both came up with same point and yes Bonnie, initially I was taking my. How cool is that???? Which one is supposed to be my config file here? Jeff, you don't see it because you have your folder view set to not display file extensions. You can change that with Folder options.

The Config file is the xxxImport. When a project is compiled to an EXE, the app. Config is just something that VS uses, you don't need to bother with that one. There is no batch file, you have to create your own. That's it. My blog post shows both the Install and the Uninstall batch files. Thank you Bonnie. The batch file is an interesting second way of installing the service and still using "installutil" for it.

Another option is ServiceStartMode. I usually set it to Manual in my code, because I don't want my service starting accidentally all by itself server reboots itself or something while I'm still in the process of testing it. It's easy enough to set it to Automatic once it's been installed and I'm done testing and it's ready for "production". To do that, right-click the Service and and choose Properties and change the Startup type the General tab, it's the first one that shows in the Properties window.

You can also change the Recovery options from the Recovery tab. In my case, our Services should never fail so I always set all three failure options to "Restart the Service" and check the "Enable actions for stops with errors" box. However, I don't have the time and interest to do it right now, so implementing it is left as an exercise for the reader :.

If you read this far you should probably follow me on Twitter or check out my other blog posts. I no longer have comments on this blog, but you can send me an email if you have some comments about this page. Bot to be available as a Windows Service, but I also wanted to be able to debug it without the hassle, so here's what I came up with: using System; using System.

OnStart args ; Console. WriteLine "Press any key to stop program" ; Console. Read ; service.



0コメント

  • 1000 / 1000