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. Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事

    Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...

  2. C#连接Sqlite 出现:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。的解决方案

    C#连接Sqlite 出现: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集.的解决方案 C#连接sqlite数据库代码 ...

  3. Java_Web三大框架之Hibernate+jsp+selvect+HQL查询数据

    俗话说:"好记性不如烂笔头".本人学习Hibernate也有一个星期了,对Hibernate也有一个初步的了解.下面对Hibernate显示数据做个笔记,使用租房系统的Hibern ...

  4. Linux内存压力测试-memtester工具

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  5. JavaScript学习书签

    JavaScript常用正则表达式 闭包 JavaScipt DOM 变量提升

  6. Mysql 在Linux下的安装

    1.获取mysql源码 wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49.tar.gz 3.添加mysql用户和用户组,创建 ...

  7. 用shell编写dhcp自动获取脚本

    #!/bin/bash#net=$(ifconfig ens33 | awk -F'[ .]+' '/inet\>/{print $3"."$4"."$5 ...

  8. uvalive 3231

    3231 - Fair ShareAsia - Seoul - 2004/2005You are given N processors and M jobs to be processed. Two ...

  9. jquery 对 table 的操作

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  10. $(document).ready(function(){}) 与 window.onload = function(){} 区别

    $(document).ready(fucntion(){ //在页面dom结构加载完毕后执行代码, }) window.onload = function(){ //页面所有内容加载完毕后,执行代码 ...