Adding windows service to a GUI-Windows application project - c#

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.

Related

deploy simple c# app to another computer from Visual Studio 2017

I have written a simple c# app to let my son do some stats on dice rolling for a game he is developing.
I created a simple interface using Visual Studio 2017 and Universal Windows.
After loads of silly errors - and lots of less silly ones, I finally have an app that runs as I want it to in debug mode.
I now want to publish it so he can copy the files and run it on his Windows computer.
Now I am sure this is really easy - but after 2 days of google searching, MSDN searching and searching this forum I am no nearer knowing what I need to do.
I have changed the solution configuration to Release and the platform to x86. I have run Build and Deploy solution form the build menu and have loads of files in my bin/x86/release file partial list from windows explorer.
But none of the exe files seem to do anything (the app5.exe waits a while sometimes, but no sign of the app anywhere).
I am obviously missing some critical preparatory step, but I can't find out what it is.
Some of the MSDN notes talk about Publish (this is greyed out or not present, but Deploy appears).
Can anyone please point me to some idiot proof documentation to help me work out what I should be doing.
I am new to Visual Studio, new to C# and new to windows app development - so my app has been pulled together from web research and using common sense :)
I have assumed that what I am trying to do is pretty easy - but I am not yet convinced.
To sell your Universal Windows Platform (UWP) app or distribute it to other users, you need to package it. If you don't want to distribute your app through Microsoft Store, you can sideload the app package directly to a device. Since you used the VS, you can follow this document to package a UWP app with Visual Studio: https://learn.microsoft.com/en-us/windows/uwp/packaging/packaging-uwp-apps#sideload-your-app-package.
After you package your app, if you want to sideload your app, you can follow the How do I sideload an app on desktop part in the following document:
https://learn.microsoft.com/en-us/windows/application-management/sideload-apps-in-windows-10#how-do-i-sideload-an-app-on-desktop
More details, you can get from this topic:
https://learn.microsoft.com/en-us/windows/uwp/packaging/

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?
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

C# service crashes on uninstall

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.

Creating a "sure to run" app / installer in VS2010

I need to create a simple installer of sorts for a different application. That other application already has it's own simplistic installer, and I don't want to meddle with it.
The reason for my own installer is to allow the user to install SQL Express if (s)he so chooses, and also to pre-install any other basic requirements for such a procedure.
At the moment, here's where I'm at:
I've created a single Windows form application, with big buttons (this is for a user which likely won't be very good with computers) to install SQL Server Express (using silent install with a predefined set of arguments) or the actual application, along with some helpful text to let the user know what's going on. Something along the lines of Visual Studio autorun window.
I've also added the standard set of pre-requirements to the application (.NET, Windows Installer).
Everything works OK if I run the app by using the executable. HOWEVER, if I publish it to create a ClickOnce application (so the pre-requisites are installed when needed) and run it, it stops running other installers.
EDIT: Apparently the problem with not being able to run other application from a ClickOnce application is only on my end, and probably deserves a new question, not necessarily here on StackOverflow (perhaps on MSDN forums?).
In your program before launching the installer you can check if .NET is installed. Its pretty easy to check if a particular s/w or a version of s/w is installed. Write a program that will check HKEY/LocalMachine/Microsoft/Windows/CurrentVersion/Uninstall and in that there will be list of programs that have been installed on that machine. If you find then go ahead with your install else suggest user that he needs to install pre req.
When you create a setup project you can right click on the setup project, go to properties, and click on pre requisites. In that you can mention which version of .NET framework is needed and then give the location of the framework. In this link look for Huggy Bears response eggheadcafe.com/community/aspnet/2/10131905/setup-project.aspx
I've settled for a "Click Once" application. It can install all required .NET components needed for it to run, thus becoming an "sure-to-run-non-native-C++-code-splash-screen".
Granted, there ARE issues with Click Once, but this is far better than nothing. It's also better than running C++ or unmanaged code applications. ;)

Disable autostart of integrated webserver in visual studio 2005

Is there a way to run a project inside a solution that has more one project (read: web applications) and avoid the autostart feature of the integrated asp.net webserver in Visual Studio?
I have a big solution that has Windows forms projects, Windows Services projects, Web applications, Windows Libraries etc., sometimes I just want to debug the desktop application but each web application project starts a webserver and that slows down the execution process (when starting the application of course) and I would like those webservers to be disabled since im not using/debuging those projects but some other developers in my team are.
Thanks.
Select the project you don't want to start up automatically in solution explorer and in the properties pane set "Always Start When Debugging" to false
You could just create a new solution with only the project(s) you are working on.

Categories