user3407039
user3407039

Reputation: 1335

Creating a windows service to run a Visual Studio solution

OK, I have never made a windows service before so sorry if this question seems a bit dumb. I currently have a windows form application with a few buttons that perform certain operations. When the buttons have been clicked these operations currently would run infinitely using a timer that i set up.

I want to set this up as a windows service, but do not really know how to. There are a lot of examples of creating a service as another project, but is this what I want or can i include it within my existing project? How would i take the process of actually having to click on these buttons once and placing that within the service?

Upvotes: 2

Views: 1564

Answers (3)

Jonas_Hess
Jonas_Hess

Reputation: 2018

How-To create a C# Windows-Service with Topshelf Console-Application

Steps:

  1. Start Visual Studio and create a new C# Console-Application
  2. Right click on references and go to manage NuGet-Packages
  3. Download and install Topshelf via NuGet
  4. Paste the Code below into your application and include all imports.
  5. Switch from “Debug” mode to “Release” and build the application.
  6. Run cmd.exe as administrator
  7. Navigate the console to “.\myConsoleApplication\bin\Release\”
  8. Run the command “.\myConsoleApplication.exe install”
  9. Run the command “.\myConsoleApplication.exe start”

Code: http://pastebin.com/BAFH27wB

Upvotes: 0

jonhoare
jonhoare

Reputation: 1309

I would highly recommend creating a Console Application to run your service, and then use TopShelf.

This will enable you to run your application easily for debugging by just launching your Exe and you can write all your debug messages to the Console window, but it has built in code to allow you to install your Exe as a windows service and will use the same code.

You can read more about Topshelf at http://docs.topshelf-project.com/en/latest/overview/index.html.

  1. Create a new Console Application Project.
  2. Add the Topshelf Nuget package
  3. There is a good example of a simple program and how to wire it up at http://docs.topshelf-project.com/en/latest/configuration/quickstart.html
  4. When you have finished, you can just launch and debug your application as a console app from inside Visual Studio or from the built executable.

When you are happy that your program is working as expected, you can then install your executable as a windows service

  1. Copy your built code into a location where you want to run your service from permanently. eg C:\MyService
  2. Open a Command Prompt (With Administrative Privilages)
  3. Change directory to your Service Directory (Eg. C:\MyService)
  4. Install your service, using the following. MyService.exe install

There are several options you can pass for installing your service, which you can find here. http://docs.topshelf-project.com/en/latest/overview/commandline.html

Upvotes: 2

Pawel Rosinski
Pawel Rosinski

Reputation: 94

You should try to use Topshelf in your C# application. It is really easy to use and well documented. When you add this nuget package into you project and configure it in C# code, then simple command

you_app.exe -install

is everything you need. For more details please have a look here:

http://topshelf.readthedocs.org/en/latest/index.html http://topshelf.readthedocs.org/en/latest/configuration/quickstart.html

Upvotes: 0

Related Questions