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?
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.
Create a new Console Application Project.
Add the Topshelf Nuget package
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
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
Copy your built code into a location where you want to run your service from permanently. eg C:\MyService
Open a Command Prompt (With Administrative Privilages)
Change directory to your Service Directory (Eg. C:\MyService)
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
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
How-To create a C# Windows-Service with Topshelf Console-Application
Steps:
Start Visual Studio and create a new C# Console-Application
Right click on references and go to manage NuGet-Packages
Download and install Topshelf via NuGet
Paste the
Code below into your application and include all imports.
Switch from “Debug” mode to “Release” and build the application.
Run cmd.exe as administrator
Navigate the console to
“.\myConsoleApplication\bin\Release\”
Run the command
“.\myConsoleApplication.exe install”
Run the command
“.\myConsoleApplication.exe start”
Code: http://pastebin.com/BAFH27wB
Related
I am trying to look out for creating C# Project in Visual Studio Code with .csproj extension but unable to find anything relevant for the same.
Please share the helpful with prerequisites for the same.
you can use Yeoman on Terminal (of VS Code or any terminal write):
yo aspnet
then you choice what you need to create, I assume you have installed Core and Yoeman:
What type of application do you want to create? (Use arrow keys)
❯ Empty Web Application
Empty Web Application (F#)
Console Application
Console Application (F#)
Web Application
Web Application Basic [without Membership and Authorization]
Web Application Basic [without Membership and Authorization] (F#)
(Move up and down to reveal more choices)
If you want to create Web application choose it and press ENTER answer the name you wish (test_web my example) and then push ENTER:
create test_web/.gitignore
create test_web/Program.fs
create test_web/Startup.fs
create test_web/test_web.fsproj
create test_web/web.config
create test_web/Properties/launchSettings.json
create test_web/README.md
create test_web/runtimeconfig.template.json
create test_web/global.json
after to create the web application test_web you need execute:
cd test_web
dotnet restore
dotnet run
To run from VS Code, you only have to open the folder test_web. Debugger is recognize .Net projects then you only push F5 to run from VS Code.
To install yeoman if you dont have:
npm install -g yo bower
web reference
you need NodeJs to use yeoman and npm.
anyway you have option to use monoDevelop in Ubuntu.
I tested it and it is working on Ubuntu for me.
I have a GUI based windows application which is an .exe application(TestServer.exe )application. Now I need to add a windows service to this project solution.
So the solution would then build two executables. The first is the one it is currently building using the UserApplication; MyTestServer.exe
The second would be the new Windows Service; MyTestservice.exe.
Both would share the MyTestServerLibrary and our 3rd party dlls.
How can i do that in C# - Visual studio 2010?
As #nvoigt said, you simply add another project to your solution. If you'll right-click on your solution in the Solution Explorer window, select the Add|New Project... menu option. In the resulting dialog, select the Windows Service project type, give it a name, and press OK. Note that this step assumes you have one of the paid versions of Visual Studio 2010. My recollection is that the Windows Service project type is not delivered with the Express version.
From there, you simply build out your Windows service project just like you would any other project. If you've never done this before, I've got a set of instructions for how to do this here. The instructions target Visual Studio 2008 (I really need to update it...), but they are practically identical for Visual Studio 2010.
At this point, when you build your project, you'll get your TestServer.exe, any other C# assemblies that are part of your solution, and the newly-added Windows service executable. By default, running the Windows service directly from the Visual Studio debugger won't work for two reasons. First, your solution probably has TestServer.exe marked as the startup project, which simply means that when you press F5 to start debugging, the TestServer.exe will be run. But even if you change the startup project to be the new Windows service, it still won't work because services don't start the way normal Windows applications do. To get around this, you can look at the instructions here for how to have your Windows service operate in an "interactive" mode, which will let you debug it like any other application. For my purposes, though, I prefer to debug my Windows service when it's actually running as a Windows service. To do this, just put a call to System.Diagnostics.Debugger.Launch() in the constructor for your Windows service. Providing you are an administrator on your system, this will give you the opportunity to jump into a debug session when you start the service from the Services console.
This leads directly to the point of installing the service. To actually run a Windows service, it has to be installed on the system. For .NET-based Windows services, you can use the Microsoft-provided InstallUtil.exe to do this. If you open the Visual Studio command prompt from the Start menu, it's available in the directory path. I prefer the solution that Marc Gravell suggested to have the Windows service install/uninstall itself. I've got a set of instructions for how to do that here.
That should get you started. There are many good answers regarding Windows services on SO, so if you get stuck, be sure to search for it on this site. HTH.
I've been working to update a C# service with a new Visual Studio 10 Installer. The old installer is a batch based installer that is a pain to use with Windows 7 and higher, but still works correctly (for both install and uninstall).
I created the new Visual Studio Installation project using this as my guide.
The solution includes a small application used to show the service status in the tray. It's output is added to the installation project and included in all of the custom actions alongside the service.
The service installs and runs correctly with my new installer, but uninstall causes a happy little blue screen of death. The dump shows the crash is coming from ntoskrnl.exe+22fa3 (Critical_Object_Termination).
What methods can be used to track down a crash that occurs during uninstall?
If the service can be stopped, then there is something really aweful in your custom action. Custom actions shouldn't be needed in the first place so try this tutorial instead.
I have seen multiple Windows Services getting installed in my computer with just an Setup.exe or .msi... is it something special that it needs to be done with the setup project to let such Windows Services installed in a easy way without Installutil.exe?
I have tried and it just doesn't install the Service. I know how to use Installutil but that I would think it's just for testing. I need to be able to install my windows service with an Setup.exe because I want to distribute my Service with its configuration Tray app.
What would be the correct way to install the service and then my app? They both use a same DLL created in the same solution.
Problem while Building a Setup Project for a windows Service?
I looked at the link above but its about the Setup Project which I already know how to do, the problem is how to make my Setup project working... my DLL is getting up in place, the Tray app is working as well, the problem is the Service not getting installed it is just giving me the .exez
but I need it to be installed correctly like be up and running at reboot and in the Service Control Manager.
Option 1
1. Download Wix
2. Download and install Wix Windows Service Setup Project Template
3. Create a new wix setup project and see what you get
This is the easiest way to create a *.msi without any option windows
Option 2
Follow this blog post. Same as option 1 but without the project template. If you go the WiX route sooner or later you will see that the project template provides basic stuff. If you want more advanced stuff you need to learn WiX a little bit and use its candle.exe, light.exe, pyro.exe etc.
Option 3
I wrote a short post how I usually do this.
Good luck
Here is a simple template for creating a *.msi installer for a Windows Service with WiX:
http://www.schiffhauer.com/wix-template-for-installing-a-windows-service/
I was able to specify my DLL dependencies with File tags.
I currently have 3 projects which produces 2 executables
Class library to do work
Windows Service project that uses library #1
WPF App that uses the library #1
The class library is used to perform some client-server interaction.
What I want is to only have 2 projects, the class library, and another that produces an executable that can be run as a Console, WPF, or a Windows Service. Is it possible (or advisable) to do it this way? What should the project output type be?
The main reason is that we have automated scripts to run that expect some fixed file paths. I want to avoid having to duplicate the scripts for the WPF executable and the Service/console executable.
I'm not sure about being able to do WPF, but TopShelf will allow you to run an executable as a console application or as a service.
http://topshelf-project.com/
There are two ways to use TopShelf - exe as a service or using shelving.
To run as a console application you just run yourprogram.exe.
To install as a service it's simply yourprogram.exe install.
I know this question is a bit old, but...
You can start a new project as a WPF project, then right-click it in the solution explorer and change "Start As" to console application. Your WPF app will still run exactly the same, but a console window will appear where you can use things like Console.WriteLine() calls.
You could then accept a command-line parameter that tells your application to suppress the rest of the UI, leaving only the console part. I don't think this will allow it to run as a service, though.
You have to go for Windows Communication Foundation aka WCF, Click link to learn basics
http://msdn.microsoft.com/en-us/netframework/dd939784