0

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?

1 Answers1

3

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.

Community
  • 1
  • 1
Matt Davis
  • 43,149
  • 15
  • 89
  • 118
  • Many Thanks for the reply. I have followed the instructions as mentioned above. In continuation with my above query I would like to know how can i control my TestServer.exe application from the newly created winodows service? – user3894187 Aug 04 '14 at 12:17
  • I will be glad to answer your question, but not in a comment as there's too little space. Ask a question, and then put a link to it here so I can find it. – Matt Davis Aug 04 '14 at 14:28
  • I have asked the question in this link https://stackoverflow.com/questions/25131036/control-my-application-from-a-windows-service-c-sharp – user3894187 Aug 05 '14 at 04:11