?
Solved

Not sure where errors are with ManagementObjectSearcher in Service

Posted on 2005-03-31
2
Medium Priority
?
553 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
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

621 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