https://docs.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase.onstart?view=netframework-4.7.1#System_ServiceProcess_ServiceBase_OnStart_System_String___

https://stackoverflow.com/questions/158772/handle-exception-on-service-startup

If the main thing you want is for the Services window to report that there was an error, from what I've tried (.net 3.5 on Windows 7), the only way to do this is by setting the ExitCode. I recommend setting it to 13816, since this results in the message, "An unknown error has occurred." See the windows error codes.

The sample below accomplishes three things.

1 - setting ExitCode results in a useful message for the end-user. It doesn't affect the Windows Application log but does include a message in the System log.

2 - Calling Stop results in a "Service successfully stopped" message in the Application log.

3 - throwing the exception results in a useful log entry in the Application log.

protected override void OnStart(string[] args) {

  try {
// Start your service
}
catch (Exception ex) {
// Log exception
this.ExitCode = ;
this.Stop();
throw;
} }

Handle exception in ServiceBase.OnStart method的更多相关文章

  1. Caused by: org.apache.velocity.exception.MethodInvocationException: Invocation of method 'getUser' in class org.uncommons.reportng.ReportMetadata threw exception class java.net.UnknownHostException :

    Running TestSuite [TestNG] [WARN] Ignoring duplicate listener : org.uncommons.reportng.HTMLReporter ...

  2. ServiceBase.OnStart 方法

    msdn 解释 派生类中实现时,在由服务控制管理器 (SCM) 或在操作系统启动时 (对于自动启动的服务) 时,将启动命令发送到服务时执行. 指定当服务启动时要执行的操作. 命名空间:   Syste ...

  3. python3.x + selenium 3.x 遇到的问题:Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x0045E450>>

    我解决了!!!from selenium import webdriverimport timedr = webdriver.Firefox(executable_path = '/Users/jin ...

  4. How to Handle Exception

  5. Exception Handling in ASP.NET Web API

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...

  6. [转]java-Three Rules for Effective Exception Handling

    主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in ...

  7. Exception (2) Java Exception Handling

    The Java programming language uses exceptions to handle errors and other exceptional events.An excep ...

  8. Exception (3) Java exception handling best practices

    List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...

  9. 一个servlet处理多个请求(使用Method的反射机制)

    方法一 可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法.达到一个servlet处理多个请求的目的 test.jsp: <%@ page language= ...

随机推荐

  1. js 学习笔记---BOM

    window对象 1. window 对象是Global对象,在全局作用域中声明的变量和函数都可以通过window.来访问.跟直接在window上添加属性效果一样.唯一的区别就是delete时,如果是 ...

  2. Stanford coursera Andrew Ng 机器学习课程第二周总结(附Exercise 1)

    Exercise 1:Linear Regression---实现一个线性回归 重要公式 1.h(θ)函数 2.J(θ)函数 思考一下,在matlab里面怎么表达?如下: 原理如下:(如果你懂了这道作 ...

  3. 微信小程序获取二维码并把logo替换为自己的头像

    $avatarUrl = 'http://cms-bucket.nosdn.127.net/2018/05/28/a1a44ffdc2d24f928c1860d4fbf54703.jpeg?image ...

  4. css3 animation 中的 steps

    steps Specifies a stepping function, described above, taking two parameters. The first parameter spe ...

  5. Autofac 控制反转

    class Program { static void Main(string[] args) { IContainer container = Init(); Go(container); Cons ...

  6. HUD 1426 Sudoku Killer (DFS)

    链接 : Here! 思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数 ...

  7. 五、Scrapy中Item Pipeline的用法

    本文转载自以下链接: https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/item-pipeline.html https://doc.scra ...

  8. SpringBoot杂记

    一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...

  9. sql server的 between and 日期问题

  10. 【Codeforces 126B】Password

    [链接] 我是链接,点我呀:) [题意] 给你一个字符串s 让你从中选出来一个字符串t 这个字符串t是s的前缀和后缀 且在除了前缀和后缀之外的中间部位出现过. 且要求t的长度最长. 让你输出这个字符串 ...