I am not able to start the service through command prompt. I got error System.NullRederenceException: Object reference not set to be an instance of an object

94 views
Skip to first unread message

Rajamanickam Thangavel

unread,
May 14, 2016, 9:30:07 PM5/14/16
to topshelf-discuss
Hi 
I heard my sr techie about topshelf windows service. i did the below coding logic, code is compiled and working fine, but not able to start the service in command prompt administrator mode. how to fix this. I tried sample service from http://www.christophdebaene.com/blog/2011/03/16/create-a-net-windows-service-in-5-steps-with-topshelf/ but not able to start from command prompt.

pl let me know where is the problem and appreciate your help. thanks

Program cs:
class Program
    {
        static void Main(string[] args)
        {
            ILog _log = LogManager.GetLogger(typeof(WorkLoadService));
            try
            {
                XmlConfigurator.ConfigureAndWatch(
                new FileInfo(".\\log4net.config"));

                var host = HostFactory.New(x =>
                {
                //x.EnableDashboard();
                x.Service<WorkLoadService>(s =>
                    {
                        s.SetServiceName("WorkLoadService");
                        s.ConstructUsing(name => new WorkLoadService());
                        s.WhenStarted(tc =>
                        {
                            XmlConfigurator.ConfigureAndWatch(
                                new FileInfo(".\\log4net.config"));
                            tc.Start();
                        });
                        s.WhenStopped(tc => tc.Stop());
                    });

                    x.RunAsLocalSystem();
                    x.StartAutomatically();
                    x.SetDescription("WorkLoadService Description");
                    x.SetDisplayName("WorkLoadService");
                    x.SetServiceName("WorkLoadService");
                });

                host.Run();
            }
            catch(Exception ex)
            {
                _log.Error(ex.Message);
                _log.Info(ex.Message);
            }
        }
    }

public class WorkLoadService
    {
        private Timer _timer = null;
        readonly ILog _log = LogManager.GetLogger(typeof(WorkLoadService));

        public WorkLoadService()
        {
            double interval = 10000; //TBD need to take from config file.
            _timer = new Timer(interval);
            _timer.Elapsed += new ElapsedEventHandler(OnTick);
        }

        protected virtual void OnTick(object sender, ElapsedEventArgs e)
        {
            _log.Debug("Tick:" + DateTime.Now.ToLongTimeString());
            //using(Queueing queueing = new Queueing())
            //{
            //    queueing.ProcessStart();
            //}
           
        }

        public void Start()
        {
            _log.Info("WorkLoadService is Started");

            _timer.AutoReset = true;
            _timer.Enabled = true;
            _timer.Start();
        }

        public void Stop()
        {
            _log.Info("WorkLoadService is Stopped");

            _timer.AutoReset = false;
            _timer.Enabled = false;
        }


    }

screen shot

event log:
- System
- Provider
[ Name] .NET Runtime
- EventID 1026
[ Qualifiers] 0
Level 2
Task 0
Keywords 0x80000000000000
- TimeCreated
[ SystemTime] 2016-05-15T00:59:36.000000000Z
EventRecordID 221424
Channel Application
Computer Infinite-lp0133
Security
- EventData
Application: ICS.MDE.Win.WorkLoad.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException at Topshelf.WindowsServiceCode.HostInstaller.Install(System.Collections.IDictionary) at System.Configuration.Install.Installer.Install(System.Collections.IDictionary) at System.Configuration.Install.TransactedInstaller.Install(System.Collections.IDictionary) Exception Info: System.InvalidOperationException at System.Configuration.Install.TransactedInstaller.Install(System.Collections.IDictionary) at Topshelf.Hosts.InstallHost.<Run>b__6(System.Configuration.Install.TransactedInstaller) at Topshelf.Hosts.AbstractInstallerHost.WithInstaller(System.Action`1<System.Configuration.Install.TransactedInstaller>) at Topshelf.Hosts.InstallHost.Run() at ICS.MDE.Win.WorkLoad.Program.Main(System.String[])  

- System
- Provider
[ Name] Application Error
- EventID 1000
[ Qualifiers] 0
Level 2
Task 100
Keywords 0x80000000000000
- TimeCreated
[ SystemTime] 2016-05-15T00:59:37.000000000Z
EventRecordID 221425
Channel Application
Computer Infinite-lp0133
Security
- EventData
ICS.MDE.Win.WorkLoad.exe
1.0.0.0
5737c99b
KERNELBASE.dll
6.1.7601.23418
5708a7e4
e0434352
0000c54f
1ac0
01d1ae450895a792
C:\Development\CareNet\SourceCode\Windows Service\WorkLoad\ICS.MDE.Win.WorkLoad\bin\Debug\ICS.MDE.Win.WorkLoad.exe
C:\Windows\syswow64\KERNELBASE.dll
4b44d29d-1a38-11e6-82b3-00607349ee54

Chris Patterson

unread,
May 14, 2016, 9:52:02 PM5/14/16
to topshelf...@googlegroups.com
You might just review the documentation and make sure you haven't missed anything.


A null reference exception on the installer seems really outside the expected behavior, particularly from the command-line.

--
You received this message because you are subscribed to the Google Groups "topshelf-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topshelf-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rajamanickam Thangavel

unread,
May 14, 2016, 10:03:38 PM5/14/16
to topshelf-discuss
Hi chris
i tried same example TownCrier (no modification), but while start service from command prompt administrator is not started. see the attached screen shot.
topshelf.png

Travis Smith

unread,
May 14, 2016, 11:45:50 PM5/14/16
to topshelf-discuss
So there could be something messed up with you administrator profile. What OS version is this? Can you duplicate it on another machine? Have you installed a Topshelf logging package and examined or attached the logs? What version of .NET and Topshelf are you using?
--
-Travis
Reply all
Reply to author
Forward
0 new messages