.NET ServiceController.WaitForStatus ignores timeout - c#

I've got a WinForm application and a service that does some work from the application on a server. I want the user to able to control the service from the application, so I added a ServiceController to do all the work (Start, Stop, Restart at first only). Everything works fine so far but while testing different scenarios I encountered a problem: My service is running on a server, the application is running on a client in the same network. I connect to the service and open the ServiceController.
I then shut down the server (VM) where the service is running and trigger the stop method from the client. I use the WaitForStatus method with a timeout, problem is: the timeout is seemingly ignored by the app:
public void StopService()
{
if (this._serviceController.CanStop &&
(this.ServiceStatus == ServiceControllerStatus.Running || this.ServiceStatus == ServiceControllerStatus.Paused))
{
this._serviceController.Stop();
this._serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
}
}
In my case, the methods seems to try to stop the service for around 90 seconds and then throws an InvalidOperationException, which I can handle but I don't want the user to wait 90 seconds.
I think my question basically is: What happens when the timer (30 seconds in this case) runs out? Shouldn't the code just continue to run? And when does this function throw an TimeoutException? MSDN says when "The value specified for the timeout parameter expires." - but it seems like this doesn't mean after the value reaches zero.
Can someone enlighten me?

When we specify TimeSpan, WaitForStatus will rise timeout exception after waiting for given time but it seems to be you having exception on privileges.
please read this answer.

Related

How to trigger failure in a windows service?

For every windows service you can define "recover policy" indicating what to do in case of a failure.
This works for when the service fail to start, but how can I trigger "failure" if the service started successfully but did something wrong while running? I tried to throw exception but the service controller just hides it, I tried to set exit code to 1 and call Stop() but it just stops gracefully..
Is there a way to tell windows "this service has crashed and want to use the recovery policy" from code and after it had successfully started? If its not possible to trigger the recover policy after started that's OK, but whats the best way to stop while indicating windows there was an error?
Thanks,

RedirectToAction does not have end response parameter. results in throwing 'Thread was being aborted" exception

I am working on MVC4 web application.
The current method RedirectToAction doesn't have an optional parameter to specify whether or not to end the response, whereas the Response.Redirect method does. Without setting the endResponse parameter to false (it is true by default), the thread gets aborted. This means every time I call RedirectToAction, I end up with a "Thread was being aborted" message in the event log.
This is explained here
https://support.microsoft.com/en-us/kb/312629
how to get workaround for this issue?
I think i have got fix for this.
before this i have some recommendations when you have long running process
1) Use Fix mentioned in https://support.microsoft.com/en-us/kb/312629
2) Try to move long running code out of IIS, try to use service which will run in background.
For those who cant change existing code here is quick and dirty solution.
Increase idle time out in IIS (app pool of your application => right click=>advanced setting ) in minutes to whatever you think enough to complete the long running process.

Silverlight 5 - WCF Service Call - CompletedEvent argument e has an error

I have an Silverlight application (Silverlight 5) and communicating through regular basic Service for normal operations (no database involved).
Problem:-
Before calling the service I just show the Loading (Busy Indicator control) and then calls the Service. Once it is back from the service (inside the completed event) I used to hide the Busy Indicator and performs other required operations.
In my case, Service executes all the logic successfully and returns true within max 2 minutes. But after that the logic that is placed inside the completed event is not getting executed always.
Note: Out of 3 times 2 times it works and once it fails. i.e 2 times it executes the logic perfectly and sometimes it will not do anything and just shows the progress bar. There is no pattern as such.
Also the important factor is IE7 and Chrome always works, IE8 we have this issue. Sometimes it works sometimes not. Very inconsistent !!!
I checked my code there seems to be no error and the service method on the server side is getting executed perfectly all the time.
I have cross verified the timwout parameters Client as well as Service and no problem and just to let you know I have another operation which takes around 6 minutes working fine in the same application.
Any Ideas would be highly appreciated.
Technical Details: Silverlight 5, WCF, In-Browser app, No database interactions.
Finally i figured out that it was due to the service client config file's receive timeout was the one which was creating the problem. Whenever it takes more than the defined the time period it just does not come back or no response and silverlight was not throwing up that error. Increasing the ReceiveTimeout helped me to resolve this. Thanks for all your help.
Wrap your service call with a "finally" to close your Busy indicator:
try
{
// Notify Busy is true
// Define Completed
// OnCompleted
// Notify Busy is false
// Make Service call
}
catch (Exception ex)
{
// Check for Timeout
// Handle Error
}
finally
{
// Notify Busy is false if Exception
}

How to programmatically create pause in Windows Service app?

I am creating a Windows Service app that I would like to have programmatically pause when either a system error, odbc connection, or missing file error occur while . I was wondering if anyone knows how to do this? The Windows service app uses an odbc connection and datareader to connect to an MS Access database and an Oracle table, so there are the probable errors that I would be handling with those, I just want to allow a pause for the user handle the errors if/when they occur.
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutValue);
service.Pause(); //or whatever you want here.
sevice.WaitForStatus(ServiceControllerStatus.Paused, timeout);
...
Then to restart, do the same thing except for
service.Continue();
sevice.WaitForStatus(ServiceControllerStatus.Running, timeout);
You can do this for any state you want. Check out the msdn documentation by googling SeviceController. It will be the first result returned.
Also, you will need to handle the OnPause and OnContinue events in your service.
Have you tried?
System.Threading.Thread.Sleep(1000); // sleep for 1 second
Adjust the 1000 to 1000 times however long you want it to sleep in seconds.
Assuming that your service has a continual loop that checks for data, add a check to an external source for pause/continue commands. This source can be a message queue like MSMQ or a database table.
I implemented something along like this by having my service continually check a table for commands, and reporting its status in another table. When it gets a start command it launches a processing loop on another thread. A stop command causes it to signal the thread to gracefully exit. The service core never stops running.
The user interacts via a separate app with a UI that lets them view the service's status and submit commands. Since the app does its control via a database it doesn't have to run on the same machine that the service is running on.

Error while stopping windows service

I am working on Windows Service. It works fine. When i am trying to stop the service from services.msc, it throws the following error:
Windows could not stop the xxx service on Local Computer.
The service did not return an error. This could be an internal Windows error or an internal service error.
If the problem persists, contact your system administrator.
If I try to stop it again, it takes lots of time and then throws the error as below:
Windows could not stop the xxx service on Local Computer.
Error 1061: The service cannot accept control messages at this time.
At this point, the service has stopped. But if I try to reinstall the service, it throws another error:
Windows could not stop the xxx service on Local Computer.
Error 1001: The specified service is marked for deletion.
After closing services.msc, it lets me reinstall the service and again things start working fine.
In OnStop(), I am doing some lengthy operations and it takes time to complete.
Any idea how I can make the whole thing go smoothly?
--Edit--
This is what my OnStop method looks like:
protected override void OnStop()
{
base.OnStop();
//Some lengthy operation
}
The windows service have a default timeout in onstart and onstop events. Normally if you are doing time consuming operations in any of these events start a new thread and let the operation to perform in background.
Usually the OnStart as well as OnStop events within the windows service are used to initiate a process and the time consuming process will carryout it's execution within a child thread.
Hope this will solve your issue..
There is a registry entry that controls how much time windows gives services to shut down before giving up: http://support.microsoft.com/kb/146092
Trivially, increasing this time would fix the issue, assuming that your service is actually shutting down after that long operation.

Categories

Resources