SlideShare a Scribd company logo
WIN SERVICE BEST PRACTICE
Yu Guan | Microsoft MVP
Meet Yu Guan | @askguanyu
twitter.com/askguanyu
linkedin.com/in/yuguan
askguanyu.wordpress.com
devlib.codeplex.com
Microsoft MVP
Speaker, Blogger, Hacker, Coder, Maker, Gamer
Azure, IoT, Cross-platform Apps, GIS, Services
WINDOWS SERVICE GUIDELINE
 Implementation Segmentation
 Installation
 Debugging
Implementation Segmentation
  You should separate business logic into individual projects.
INSTALLATION
 Need installer component and mark the class attribute as [RunInstaller(true)]
 You can use command line to install/uninstall windows service.
 You can use .net feature to install/uninstall windows service.
Debugging
 Run in console
 Attach process
 Debugger.Launch() and Debugger.Break()
 Thread.Sleep(xxx)
DevLib.ServiceProcess
 NuGet: https://www.nuget.org/packages/DevLib.ServiceProcess/
 Source code:
https://devlib.codeplex.com/SourceControl/latest#Main/Source/DevLib.ServicePr
ocess/
 Easy to port
 Solve pain points
DEMO
APPENDIX
Wording Intent
 Do... This standard or practice should be followed in all cases. If you think that your specific application
is exempt, it probably isn't.
 Do Not... This standard or practice should never be applied.
 You should... This standard or practice should be followed in most cases.
 You should not... This standard or practice should not be followed, unless there's reasonable justification.
 You can… This standard or practice can be followed if you want to; it's not necessarily good or bad. There
are probably implications to following the practice (dependencies, or constraints) that should be
considered before adopting it.
THANK YOU!

More Related Content

PDF
1. f.y.b.com syllabus
PPTX
Windows Communication Foundation (WCF) Best Practices
PPT
Introduction To Windows Services
PDF
How to create C# Windows Service Application
PPTX
PDF
Delphi - Howto Services
PPTX
Windows Services 101
PPT
Session 2
1. f.y.b.com syllabus
Windows Communication Foundation (WCF) Best Practices
Introduction To Windows Services
How to create C# Windows Service Application
Delphi - Howto Services
Windows Services 101
Session 2

More from Yu GUAN (11)

PPTX
Canada Chinese Microsoft Tech Club Event 1
PDF
Asp netmvc e03
PPTX
You can git
PPTX
A Journey to Azure
PPTX
Dream career dot NET
PPTX
NuGet package CI and CD
PPTX
Hosting your own NuGet private repository
PPTX
Wcf best practice
PPTX
Unleash the power of code reuse - creating plugins for Xamarin
PPTX
A Journey To Microsoft Azure E00 Azure 101
PPTX
Welcome to power point
Canada Chinese Microsoft Tech Club Event 1
Asp netmvc e03
You can git
A Journey to Azure
Dream career dot NET
NuGet package CI and CD
Hosting your own NuGet private repository
Wcf best practice
Unleash the power of code reuse - creating plugins for Xamarin
A Journey To Microsoft Azure E00 Azure 101
Welcome to power point
Ad

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPT
Introduction Database Management System for Course Database
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ai tools demonstartion for schools and inter college
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Transform Your Business with a Software ERP System
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Online Work Permit System for Fast Permit Processing
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
L1 - Introduction to python Backend.pptx
Odoo Companies in India – Driving Business Transformation.pdf
PTS Company Brochure 2025 (1).pdf.......
Introduction Database Management System for Course Database
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Design an Analysis of Algorithms I-SECS-1021-03
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ai tools demonstartion for schools and inter college
Which alternative to Crystal Reports is best for small or large businesses.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Ad

Windows service best practice

  • 1. WIN SERVICE BEST PRACTICE Yu Guan | Microsoft MVP
  • 2. Meet Yu Guan | @askguanyu twitter.com/askguanyu linkedin.com/in/yuguan askguanyu.wordpress.com devlib.codeplex.com Microsoft MVP Speaker, Blogger, Hacker, Coder, Maker, Gamer Azure, IoT, Cross-platform Apps, GIS, Services
  • 3. WINDOWS SERVICE GUIDELINE  Implementation Segmentation  Installation  Debugging
  • 4. Implementation Segmentation   You should separate business logic into individual projects.
  • 5. INSTALLATION  Need installer component and mark the class attribute as [RunInstaller(true)]  You can use command line to install/uninstall windows service.  You can use .net feature to install/uninstall windows service.
  • 6. Debugging  Run in console  Attach process  Debugger.Launch() and Debugger.Break()  Thread.Sleep(xxx)
  • 7. DevLib.ServiceProcess  NuGet: https://www.nuget.org/packages/DevLib.ServiceProcess/  Source code: https://devlib.codeplex.com/SourceControl/latest#Main/Source/DevLib.ServicePr ocess/  Easy to port  Solve pain points
  • 9. APPENDIX Wording Intent  Do... This standard or practice should be followed in all cases. If you think that your specific application is exempt, it probably isn't.  Do Not... This standard or practice should never be applied.  You should... This standard or practice should be followed in most cases.  You should not... This standard or practice should not be followed, unless there's reasonable justification.  You can… This standard or practice can be followed if you want to; it's not necessarily good or bad. There are probably implications to following the practice (dependencies, or constraints) that should be considered before adopting it.

Editor's Notes

  • #6: How to: Install and Uninstall Services .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 3.5 .NET Framework 2.0  If you’re developing a Windows Service by using the .NET Framework, you can quickly install your service application by using a command-line utility called InstallUtil.exe. If you’re a developer who wants to release a Windows Service that users can install and uninstall you should use InstallShield. See 121be21b-b916-43e2-8f10-8b080516d2a0. Warning If you want to uninstall a service from your computer, don’t follow the steps in this article. Instead, find out which program or software package installed the service, and then choose Add/Remove Programs in Control Panel to uninstall that program. Note that many services are integral parts of Windows; if you remove them, you might cause system instability. In order to use the steps in this article, you first need to add a service installer to your Windows Service. See Walkthrough: Creating a Windows Service Application in the Component Designer. Windows Service projects cannot be run directly from the Visual Studio development environment by pressing F5. This is because the service in the project must be installed before you can run the project. Tip You can launch Server Explorer and verify that your service has been installed or uninstalled. For more information, see How to: Access and Initialize Server Explorer/Database Explorer. To install your service manually On the Windows Start menu or Start screen, choose Visual Studio , Visual Studio Tools, Developer Command Prompt. A Visual Studio command prompt appears. Access the directory where your project's compiled executable file is located. Run InstallUtil.exe from the command prompt with your project's executable as a parameter: Copy installutil <yourproject>.exe If you’re using the Visual Studio command prompt, InstallUtil.exe should be on the system path. If not, you can add it to the path, or use the fully qualified path to invoke it. This tool is installed with the .NET Framework, and its path is %WINDIR%\Microsoft.NET\Framework[64]\<framework_version>. For example, for the 32-bit version of the .NET Framework 4 or 4.5.*, if your Windows installation directory is C:\Windows, the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe. For the 64-bit version of the .NET Framework 4 or 4.5.*, the default path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe. To uninstall your service manually On the Windows Start menu or Start screen, choose Visual Studio, Visual Studio Tools, Developer Command Prompt. A Visual Studio command prompt appears. Run InstallUtil.exe from the command prompt with your project's output as a parameter: Copy installutil /u <yourproject>.exe Sometimes, after the executable for a service is deleted, the service might still be present in the registry. In that case, use the command sc delete to remove the entry for the service from the registry.
  • #7: How to: Debug Windows Service Applications .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 3.5 .NET Framework 2.0  A service must be run from within the context of the Services Control Manager rather than from within Visual Studio. For this reason, debugging a service is not as straightforward as debugging other Visual Studio application types. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug your application by using all of the standard debugging functionality of Visual Studio. Caution You should not attach to a process unless you know what the process is and understand the consequences of attaching to and possibly killing that process. For example, if you attach to the WinLogon process and then stop debugging, the system will halt because it can’t operate without WinLogon. You can attach the debugger only to a running service. The attachment process interrupts the current functioning of your service; it doesn’t actually stop or pause the service's processing. That is, if your service is running when you begin debugging, it is still technically in the Started state as you debug it, but its processing has been suspended. After attaching to the process, you can set breakpoints and use these to debug your code. Once you exit the dialog box you use to attach to the process, you are effectively in debug mode. You can use the Services Control Manager to start, stop, pause and continue your service, thus hitting the breakpoints you've set. You can later remove this dummy service after debugging is successful. This article covers debugging a service that's running on the local computer, but you can also debug Windows Services that are running on a remote computer. See Remote Debugging. Note Debugging the OnStart method can be difficult because the Services Control Manager imposes a 30-second limit on all attempts to start a service. For more information, see Troubleshooting: Debugging Windows Services. Warning To get meaningful information for debugging, the Visual Studio debugger needs to find symbol files for the binaries that are being debugged. If you are debugging a service that you built in Visual Studio, the symbol files (.pdb files) are in the same folder as the executable or library, and the debugger loads them automatically. If you are debugging a service that you didn't build, you should first find symbols for the service and make sure they can be found by the debugger. See Specify Symbol (.pdb) and Source Files in the Visual Studio Debugger. If you're debugging a system process or want to have symbols for system calls in your services, you should add the Microsoft Symbol Servers. See Debugging Symbols. To debug a service Build your service in the Debug configuration. Install your service. For more information, see How to: Install and Uninstall Services. Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see How to: Start Services. Start Visual Studio with administrative credentials so you can attach to system processes. (Optional) On the Visual Studio menu bar, choose Tools, Options. In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button. On the menu bar, choose Attach to Process from the Debug or Tools menu. (Keyboard: Ctrl+Alt+P) The Processes dialog box appears. Select the Show processes from all users check box. In the Available Processes section, choose the process for your service, and then choose Attach. Tip The process will have the same name as the executable file for your service. The Attach to Process dialog box appears. Choose the appropriate options, and then choose OK to close the dialog box. Note You are now in debug mode. Set any breakpoints you want to use in your code. Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints. For more information about running the Services Control Manager, see How to: Start Services. Also, see Troubleshooting: Debugging Windows Services. Debugging Tips for Windows Services Attaching to the service's process allows you to debug most, but not all, the code for that service. For example, because the service has already been started, you cannot debug the code in the service's OnStart method or the code in the Main method that is used to load the service this way. One way to work around this limitation is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this dummy service to load the service process. Once the temporary service has started the process, you can use the Debug menu in Visual Studio to attach to the service process. Try adding calls to the Sleep method to delay action until you’re able to attach to the process. Try changing the program to a regular console application. To do this, rewrite the Main method as follows so it can run both as a Windows Service and as a console application, depending on how it's started. How to: Run a Windows Service as a console application Add a method to your service that runs the OnStart and OnStop methods: Copy internal void TestStartupAndStop(string[] args) { this.OnStart(args); Console.ReadLine(); this.OnStop(); } Rewrite the Main method as follows: Copy static void Main(string[] args) { if (Environment.UserInteractive) { MyNewService service1 = new MyNewService(args); service1.TestStartupAndStop(args); } else { // Put the body of your old Main method here. } In the Application tab of the project's properties, set the Output type to Console Application. Choose Start Debugging (F5). To run the program as a Windows Service again, install it and start it as usual for a Windows Service. It's not necessary to reverse these changes. In some cases, such as when you want to debug an issue that occurs only on system startup, you have to use the Windows debugger. Install Debugging Tools for Windows and see How to debug Windows Services.