Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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::
C#
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.
Posted
Updated 21-Jan-15 5:29am
v2
Comments
Dave Kreskowiak 21-Jan-15 11:46am    
The problem appears to be in your TaskWinSvc.Run() method. The code you posted can't throw the NullReferenceException.
Sergey Alexandrovich Kryukov 21-Jan-15 15:56pm    
Agree, so I provided general recommendations on how to debug in my answer.
—SA

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
 
Share this answer
 
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...
 
Share this answer
 
Comments
Dave Kreskowiak 21-Jan-15 17:02pm    
I used TopShelf for a service I just wrote completed. That made the entire debugging process as easy as launching and debugging a Console app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900