473,657 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows service operation throws error: Operation is not valid due to the current state of the object.

I have this code, which uses WMI to operate on Windows service from C#
code.

When Service.InvokeM ethod is called, the method throws an exception:
System.Manageme nt Operation is not valid due to the current state of
the object. at System.Manageme nt.ManagementOb ject.InvokeMeth od(String
methodName, Object[] args)
Here is the method.

RequestStatus is Enum type defined in the class.

ServerName - Machine where the service might be running

ServiceName - Service name on which the operation is to be performed

OpInstructions - Operation to be performed ( StartService,
StopService )

I have tried Start, StartService, RestartService, Stop, StopService.
None have worked.

Services I tried these actions on: Alerter, Messenger, MSSQLSERVER.

Any ideas whats going wrong? Thanks.

public static RequestStatus OperateService ( string ServerName, string
ServiceName, string OpInstructions )

{

ConnectionOptio ns Options = new
ConnectionOptio ns();

Options.EnableP rivileges = true;

//Service name space: \root\cimv2

ManagementScope Scope = new
ManagementScope ( @"\\" + ServerName + @"\root\cimv 2", Options );

Scope.Connect() ;

if ( Scope.IsConnect ed )

{

ObjectQuery Query = new
ObjectQuery( "SELECT State FROM Win32_Service WHERE Name = '" +
ServiceName + "'" );
ManagementObjec tCollection Services = new ManagementObjec tSearcher (
Scope, Query ).Get();

if ( Services.Count > 0
)

{

foreach (
ManagementObjec t Service in Services )

{
Service.InvokeM ethod( OpInstructions, null );

}

return
RequestStatus.Y es;

}

else

{

return
RequestStatus.N otFound;

}
}

return RequestStatus.N o;

}

Nov 17 '05 #1
1 6969
Found the problem. My WQL query was wrong. It needed Name.

SELECT State, Name FROM Win32_Service WHERE Name = '" +
ServiceName + "'"

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15217
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000 Personal disk from the SQL Server 2000 Enterprise kit as this is reported here on the MSDN web site to be the version that is supported on Windows XP. In fact so many of you kind people confess to having succeeded in doing it. I have tried...
5
12931
by: Dhilip Kumar | last post by:
Hi All, I'm writing a Windows Service app using C#. I need to read some configuration settings before the service starts up. These settings will be used by the service in its operation. Question is, which is the best way to store & retrieve the settings? I'm thinking of storing it in the registry in HKLM\Software\ServiceName and access it using the Registry class in the "public ServiceName()" method. I'm instructing the service...
3
2502
by: THNQDigital | last post by:
Hi All, I have written VB.NET Windows Service whcih reads some information from Outlook and creates an Excel report outa it. When i make a console application and run the program everything works fine but same code does not work if i put it in an Windows service. My OS : Win2000 Pro
4
5609
by: Groundskeeper | last post by:
I can't seem to get a custom UnhandledException handler working for a Windows Service I'm writing in VB.NET. I've read the MSDN and tried various different placements of the AddHandler call, to no avail. Here's the code I'm using, all of which is in the main service class: Protected Overrides Sub OnStart(ByVal args() As String) AddHandler AppDomain.CurrentDomain.UnhandledException, _
0
1277
by: N! Xau | last post by:
Ciao, Hi, a datagrid on my form displays a table T, fine. On the form I have a button. I want to execute a query on T, just clicking that button. DB is Oracle. Button_Click code is: .... Dim QSele As String = "select field1 from T"
8
2998
by: Jim in Arizona | last post by:
I've been using an example out of a book to be able to edit the rows in a database. I am getting the following error: ======================================================== ======================================================== Server Error in '/' Application. -------------------------------------------------------------------------------- Operation must use an updateable query. Description: An unhandled exception occurred during...
0
2082
by: whizpop | last post by:
Hi, First of all, thanks for a great starter kit, now If I could just get it to work (fully). I am trying to compile and run the solution/services all on a local dev box. I am able to successfully run most of the SampleApp features (Register,Feedback,Error report, version check) The only method I can not execute is the BuyNow example. I have tried leveraging the Sharewarestarterkit.com web service to rule out locality, to no avail.
11
7905
by: Joseph Geretz | last post by:
I've been looking at two approaches for the maintenance of Session state for a Web Service application. One approach uses the old familiar Session object which I've used in the past for Web applications. As far as I can see, the Session approach is non-standard since Web Services are supposed to be agnostic with respect to their clients. It seems that cookies are outside the Web Service standard; therefore, such a Web Service application...
0
1686
by: dom_perron | last post by:
Hi, I have a problem when I try to start an application using MSMQ from a Windows Service. The application runs on a server. The client send a message to a public queue created on the server, the server process the request and return the result to a public queue on the client. All is working fine if I start my application directly on the server. But we want to embed the start of the application into a Windows service. When I install...
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.