Windows服务安装错误 [英] Windows Service installation error

查看:130
本文介绍了Windows服务安装错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法创建WindowsService.ProjectInstaller安装程序ty 
pe的实例。
System.Reflection.TargetInvocationException:调用的ta
rget抛出了异常。
使用以下
错误消息抛出内部异常System.NullReferenceException:对象引用未设置为对象的实例..
在System.Configuration的回滚阶段发生异常.Inst
all.AssemblyInstaller安装程序。
System.InvalidOperationException:无法创建WindowsSer
vice.ProjectInstaller安装程序类型的实例。
使用
抛出内部异常System.Reflection.TargetInvocationException以下错误消息:invoc
ation的目标抛出了异常。
内部异常系统。使用以下
错误消息抛出NullReferenceException:对象引用未设置为对象的实例..
安装的回滚阶段发生异常。除
之外的其他内容将被忽略,并且回滚将继续。但是,在回滚完成后,机器可能会完全恢复到其初始状态。

回滚阶段成功完成。

已完成交易安装。
安装失败,并且已执行回滚。



我的安装程序文件:

< pre lang =c#> 使用系统;
使用 System.Collections;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Configuration.Install;
使用 System.Linq;


namespace TaskWinSvc
{
[RunInstaller( true )]
public partial class ProjectInstaller:Installer
{
public ProjectInstaller()
{
InitializeComponent();
TaskWinSvc.Run();
}

private void serviceProcessInstaller1_AfterInstall( object sender,InstallEventArgs e)
{

}

private void serviceInstaller1_AfterInstall( object sender,InstallEventArgs e)
{

}
}
}



这是初始化组件方法::

  private   void  InitializeComponent()
{

this .serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this .serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this .serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this .serviceProcessInstaller1.Password = null ;
this .serviceProcessInstaller1.Username = null ;
this .serviceProcessInstaller1.AfterInstall + = new System.Configuration.Install.InstallEventHandler( .serviceProcessInstaller1_AfterInstall);
//
// serviceInstaller1
//
.serviceInstaller1.ServiceName = TaskWinSvc;
this .serviceInstaller1.AfterInstall + = new System.Configuration.Install.InstallEventHandler( .serviceInstaller1_AfterInstall);
//
// ProjectInstaller
//
this .Installers.AddRange( new System.Configuration.Install.Installer [ ] {
this .serviceProcessInstaller1,
this .serviceInstaller1});

}

请帮我解决这个错误。

解决方案

Dave Kreskowiak是对的,请看他对这个问题的评论。



你不能只看到这个bug,你必须调试它。最好是进行调试,而不是记录。

由于您可以在Visual Studio中加载项目,但安装必须由服务控制器启动,您可能需要跟踪安装过程加载它,独立启动安装然后附加调试器(Visual Studio菜单: F ile - > D ebug - >附加到 P rocess ...)。



这种类型的bug是最容易检测和修复的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:要么确保将成员/变量正确初始化为非空引用,要么将其检查为null,如果为null,则执行其他操作。



另请参阅:想要在按钮点击时显示下一条记录。但是如果下一条记录功能的条件对象引用没有设置为对象的实例 [ ^ ]。



有时候,你不能在调试器下,由于一个或另一个原因做到这一点。一个非常讨厌的情况是,只有在调试信息不​​可用时构建软件时才会出现问题。在这种情况下,你必须使用更难的方式。首先,你需要确保你永远不会通过静默处理异常来阻止异常的传播(这是开发人员对自己的犯罪,但很常见)。您需要在每个线程的最顶层堆栈帧上捕获绝对所有异常。如果处理类型 System.Exception 的异常,则可以执行此操作。在处理程序中,您需要记录所有异常信息,尤其是 System.Exception.StackTrace

http://msdn.microsoft.com/en-us/library/system.exception.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.exception。 stacktrace.aspx [ ^ ]。



堆栈跟踪只是一个字符串,显示从throw语句到处理程序的异常传播的完整路径。通过阅读,您总能找到目的。对于日志记录,使用类 System.Diagnostics.EventLog

http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx [ ^ ]。
-SA


在我的一个项目中我挣扎着服务。正如谢尔盖所​​描述的那样,调试服务并不是不可能,也不是简单,因为安装过程更少,因为你的连接时间很短。当然,您可以添加睡眠周期以延长此时间范围。

但还有另一种方法。我想你已经看到了自我安装的服务可执行文件。我不知道出于什么原因,这在.NET中默认没有实现。但你可以实现它。我在此处找到了说明: https:// groups .google.com / forum /?hl = en#!topic / microsoft.public.dotnet.languages.csharp / TUXp6lRxy6Q [ ^ ](我在文章 [ ^ ],从中可以更容易地提取)。通过实现此方法,您不仅可以轻松调试,还可以在必要时以控制台模式运行应用程序,当然您还可以自行安装可执行文件。尽管如此,如果您获得的例外情况源自服务最初运行的环境(运行用户,缺少用户配置文件),这对您没有帮助,您需要以旧方式进行调查......

Unable to create an instance of the WindowsService.ProjectInstaller installer ty
pe.
System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation.
The inner exception System.NullReferenceException was thrown with the following
error message: Object reference not set to an instance of an object..
An exception occurred during the Rollback phase of the System.Configuration.Inst
all.AssemblyInstaller installer.
System.InvalidOperationException: Unable to create an instance of the WindowsSer
vice.ProjectInstaller installer type.
The inner exception System.Reflection.TargetInvocationException was thrown with
the following error message: Exception has been thrown by the target of an invoc
ation..
The inner exception System.NullReferenceException was thrown with the following
error message: Object reference not set to an instance of an object..
An exception occurred during the Rollback phase of the installation. This except
ion will be ignored and the rollback will continue. However, the machine might n
ot fully revert to its initial state after the rollback is complete.
 
The Rollback phase completed successfully.
 
The transacted install has completed.
The installation failed, and the rollback has been performed.


My installer file:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


namespace TaskWinSvc
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            TaskWinSvc.Run();
        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }
    }
}


This is initialize component method::

private void InitializeComponent()
       {

           this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
           this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
           //
           // serviceProcessInstaller1
           //
           this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
           this.serviceProcessInstaller1.Password = null;
           this.serviceProcessInstaller1.Username = null;
           this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);
           //
           // serviceInstaller1
           //
           this.serviceInstaller1.ServiceName = "TaskWinSvc";
           this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);
           //
           // ProjectInstaller
           //
           this.Installers.AddRange(new System.Configuration.Install.Installer[] {
           this.serviceProcessInstaller1,
           this.serviceInstaller1});

       }

Please help me to solve this error.

解决方案

Dave Kreskowiak is right, please see his comment to the question.

You you cannot just see this bug, you will have to debug it. It's probably better to do the debugging, not logging.
As you can load your project in Visual Studio, but installation has to be started by the Service Controller, you may need to track the process of installation by loading it, starting installation independently and then attaching the debugger (Visual Studio menu: File -> Debug -> Attach To Process…).

This type of bug is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA


During one of my projects I struggled with services. As Sergey described, it is not impossible, still also not simple to debug a service, even less it's installation process, as you have a narrow timeframe to attach. Of course, you can add Sleep periods to prolong this timeframe.
But there is an other method. I suppose you have seen self-intalling service executables. I don't know for what reason, this is not implemented by default in .NET. But you can implement it. I have found a description here: https://groups.google.com/forum/?hl=en#!topic/microsoft.public.dotnet.languages.csharp/TUXp6lRxy6Q[^] (and I have used it in my article[^], from where it will be easier to extract). By implementig this method you have the chance not only te easy debugging, but also to run the application in console mode if necessary and of course you have a self-installing executable. Still, if the excepion you get is originating from the environment in which the service is originally running (running user, the absence of user profile), this won't help you and you need to investigate in the old fashion...


这篇关于Windows服务安装错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆