?
Solved

Not sure where errors are with ManagementObjectSearcher in Service

Posted on 2005-03-31
2
Medium Priority
?
552 Views
Last Modified: 2008-02-01
I have two errors in my errorlog when I try to start the Service.

Event Type:      Error
Event Source:      DCOM
Event Category:      None
Event ID:      10009
Date:            3/31/2005
Time:            8:24:53 AM
User:            NT AUTHORITY\SYSTEM
Computer:      
Description:
DCOM was unable to communicate with the computer root using any of the configured protocols.

And

Event Type:      Error
Event Source:      Service1
Event Category:      None
Event ID:      0
Date:            3/31/2005
Time:            8:24:53 AM
User:            N/A
Computer:
Description:
Service cannot be started. System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable.
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
   at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObjectSearcher.Initialize()
   at System.Management.ManagementObjectSearcher.Get()
   at WindowsService1.LastBootUpTask.GetLastBootupInfo() in c:\windowsservice1\lastbootuptask.cs:line 19
   at WindowsService1.Service1.OnStart(String[] args) in c:\windowsservice1\service1.cs:line 74
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)


Service1.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService1
{
      public class Service1 : System.ServiceProcess.ServiceBase
      {
            /// Required designer variable.
            private System.ComponentModel.Container components = null;
            private LastBootUpTask Bootup;

            public Service1()
            {
                  // This call is required by the Windows.Forms Component Designer.
                  InitializeComponent();

            }

            // The main entry point for the process
            static void Main()
            {
                  System.ServiceProcess.ServiceBase[] ServicesToRun;
      
                  ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

                  System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            }

            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            private void InitializeComponent()
            {
                  components = new System.ComponentModel.Container();
                  this.ServiceName = "Service1";
            }

            /// Clean up any resources being used.
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            /// Set things in motion so your service can do its work.
            protected override void OnStart(string[] args)
            {
                  // TODO: Add code here to start your service.
                      Bootup = new LastBootUpTask();
                                Bootup.GetLastBootupInfo();  ***Error is here on one line
            }
 
            /// Stop this service.
            protected override void OnStop()
            {
                  // TODO: Add code here to perform any tear-down necessary to stop your service.
            }
      }
}


LastBootUpTask.cs

using System;
using System.Management;
using System.Windows.Forms;
using System.Globalization;

namespace WindowsService1
{
             /// Summary description for LastBootUpTask.
      
      public class LastBootUpTask
      {

               public void GetLastBootupInfo()
               {
                   ManagementObjectSearcher query = new ManagementObjectSearcher
                                (@"\\root\cimv2","SELECT * FROM Win32_OperatingSystem");
            DateTime bootup;
            query.Get();
            foreach(ManagementObject mo in query.Get())
                       {
                  string DMTF = mo.Properties["LastBootUpTime"].Value.ToString();
                   bootup = ManagementDateTimeConverter.ToDateTime(DMTF);

                   MessageBox.Show(bootup.ToString("dd-MMM-yyyy"),"Shutdown Time", MessageBoxButtons.OK,
                              MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                              MessageBoxOptions.ServiceNotification);
            }
                //return bootup;
//                 MessageBox.Show(bootup.ToString("dd-MMM-yyyy"),"Shutdown Time", MessageBoxButtons.OK,
//                        MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
//                        MessageBoxOptions.ServiceNotification);
            }
      }
}
0
Comment
Question by:NewMom2Brandon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Author Comment

by:NewMom2Brandon
ID: 13673307
I fixed the issues

here is the fix to LastBootUpTask.cs

public class LastBootUpTask
{

    public void GetLastBootupInfo()
   {
       try
       {
           DateTime bootup;
           string ConfigNamespace = @"\\.\root\cimv2";
           string query = "select * from Win32_OperatingSystem";

           ManagementObjectSearcher searcher = new
      ManagementObjectSearcher( ConfigNamespace, query );
                              
           ManagementObjectCollection collection = searcher.Get();
                                                                  

           foreach (ManagementObject item in collection)
           {
      string DMTF = item.Properties["LastBootUpTime"].Value.ToString();
      bootup = ManagementDateTimeConverter.ToDateTime(DMTF);

               MessageBox.Show(bootup.ToString("dd-MMM-yyyy"),"Shutdown Time", MessageBoxButtons.OK,
           MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
           MessageBoxOptions.ServiceNotification);

           }

        catch (Exception e)
       {
             System.Diagnostics.EventLog.WriteEntry(this.ToString(),e.ToString());
             throw new Exception("Failed to get last reboot", e);
       }
    }
}
0
 

Accepted Solution

by:
PAQ_Man earned 0 total points
ID: 13676413
Question Closed, 500 points refunded.
PAQ_Man
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question