My service is running currently on
localhost:17722/Book.svc
How can my client app know if the service running before trying to consume it?
Don't check to see if a server is up before consuming it. Consume the service and handle any errors that occur during the call.
Even if you check that the service is up it can go down between the time you check that it is up and you call the service. So even if you check that it is up, you still need to handle it being down when you consume it.
The best way to check if a service is up is to try to use it.
Only some kind of ping
but without guarantee, that after you ping was ok the service will be not droped before your real request
To summarize:
Prepare you clint to handle dead service(s), faults etc
I would apply the principal of Tell, don't ask in this situation. Just try to perform whatever operation you were intending on doing, and then handle the exception if it fails.
Why would your program be consuming a service if it was not necessary to the operation of the program?
Hey service, can I invoke your 'X' method? ... no? ... okay, I didn't want to anyway :P
Well this is not possible until you try the service (or ping the server). Calling your web service from your client application is like calling any other web service, how can you know that a "google's" web service is running before consuming it?
Once I had a similar problem and I just expose an operation that returns "something" (return true for instance) that I called to know if the server application was "operational" and expecting a "timeout" or "500" error when not working.
Setup a dummy method in the svc that returns a bool or something. Then hit it on a background thread at a specified interval. If the request times out you can then handle the timeout by displaying something that says the service is unreachable.
Related
I have searched about, how to know if WCF service is down OR not and I came to know that the only way to know this is to call WCF service method. We can create a method that just return true.
In my case I want to show a message when service is down to the client and for other exceptions of WCF service, I want to show the exceptions as it is. What should be the best way to this ?
If the service is yours then go ahead and use that method that returns true.
However, you should just go ahead and consume the service, as it's not necessarily true that it will remain up the entire time that you consume it. Handle your interim timeouts and errors gracefully for all of your operations and show your messages accordingly.
One client-side thing you can do is send a WebRequest and see if it returns a HttpStatusCode.OK but it can't promise you that the service won't fail or is running.
During your consuming the service you can expect FaultException, proxy and channel state errors, invalid configuration (bindings, etc) and CommunicationException objects.
You can read more on this on MSDN.
From there:
Communication errors occur when a network is unavailable, a client uses an incorrect address, or the service host is not listening for incoming messages. Errors of this type are returned to the client as CommunicationException or CommunicationException-derived classes. Application errors occur during the execution of a service operation. Errors of this kind are sent to the client as FaultException or FaultException.
This problem can be solved only "client side", because server can reply with "true" only if WCF service is running and well configured.
All the other case should be managed from client.
In a simple way you can call that simple service if it reply it seems all OK, otherwise you'll get a faultexception or an exception that you need to catch.
Pay attention at async / await, you need to catch exception in the closer place to the web call otherwise you'll get an uncaught application exception that will crash app domain.
I have several services running. I can call everyone from a client application. I am trying to call into one service from another service (same application - they are hosted in an application for testing but can also run as a windows service).
The call I use to do this from the client is simply create the factory and CreateChannel and then open.
When I do this in a service trying to connect to another service I don't get an error it just hangs and eventually times out. I have no idea what is wrong.
I am using net.pipe://localhost/test as my endpoint and transport.
This was really stupid but (and) I will post the issue to help others that may run into this...
All of my service was running single threaded so when I called into another service it was blocking itself. I now start my threads on backgroundworker threads and the issue is gone.
Thanks
I'm writing a webservice that drops off a long-running bulk insert command to a sql db through a stored proc. I don't want the webservice hung up while waiting for a response from the db, so I'd like to just return an http response that lets the client know the request has been sent to the db after I start the task. But as soon as I return the response, the task will lose context and get trashed, right? How should I keep this alive?
In general, it's not a good idea to spin off something to do work from IIS. What happens if the AppPool restarts? What happens if there is an exception?
Instead, I would recommend writing a Windows Service and have it responsible for the work.
Based on your comments, I would see if you can ask for the following requirements (theoretically):
All external calls are done through the web service. The web service uses a separate assembly for the actual data access.
A separate windows service is used for long running processes, which would also use the same data access assembly the web service uses.
That is really the best way to go (but not necessarily doable based on requirements).
I think it's more of a architecture question than just about maintaining the 'context'. And talking about architecture, I think WCF webservices would help in your scenario.
What you would need is a service with callback contract. Where the service takes a request, returns an ack, stores the client context (for callback), and triggers off a long running database task in background. When the task completes, it reads client context and calls the callback handler with the result.
This article at MSDN suggests how to do a callback contract in webservice.
Hope this helps!
I'm programming a monitoring application that needs to display the state of several windows services. In the current version, I can know whether a service is Running, Stopped, Suspended or in one of the pending states. That's good, but I'm wondering if there is a way to test if a service is actually responding? I guess it can be in a running state but not responding at all!
I am using the ServiceController class from System.ServiceProcess. Do you think that if a service is not responding, the ServiceController.Status would return an exception?
How would you approach the problem?
Thanks
EDIT
Seems that: ServiceController.Status can return 2 types of exceptions:
System.ComponentModel.Win32Exception: An error occurred when accessing a system API.
System.InvalidOperationException: The service does not exist as an installed service.
Nothing about reactivity.
This might be obvious, but have you tried talking to the service?
There's no common way to talk to a service, so there is no way Windows can interrogate whether the service is still responding as normal. It is perfectly normal for a service to go into a complete sleep waiting for external I/O to happen, and thus Windows would not get a response while the service is actually alive and functioning exactly as designed.
The only way is to actually send a request to it, and wait for the response, and for that you need some inter-process communication channel, like:
Network
Named pipes
Messages
Basically, if you need to determine if a service is able to respond, you need to check if it is responding.
The service controller types and APIs can only provide information on the basis of the service's response to those APIs.
E.g. you can create a service which responds to those APIs correctly, but provides no functionality on even numbered hours.
In the end you need to define "responsive" in terms of the services functionality (e.g. a batch processor is processing batches) and provide a mechanism (A2A API, WMI, Performance Counters) to surface this.
I'm developing an application to achieve work in background through a Windows Service.
I've created the service thanks to the MSDN tutorial, and then I can Start or Stop it correctly.
My service also need to execute custom commands. Therefore, I've implemented the "OnCustomCommand" method in the service's class.
I can send custom execution commands with commands ID between 128 and 255, accordingly everything i've found on the net.
My problem is :
I can't execute command on the service when it's stopped. ExecuteCommand() throws an System.InvalidOperationException every time I call it on the serviceController.
Cannot control <myservice> service on computer '.'
This does not happens when the service is running.
MSDN says
When you call ExecuteCommand, the
status of the service does not change.
If the service was started, the status
remains Running. If the service was
stopped, the status remains Stopped,
and so on.
So I suppose this is actually possible to executeCommand on stopped services.
My service is installed as LocalSystem service, and the serviceController is run with administrator privileges.
Does anyone know how to resolve this?
It would not be possible to ExecuteCommand against a stopped service. I believe what the documentation you're reading is telling you is that calling ExecuteCommand will not start the service if it is stopped. It is not saying that you will still be able to call the method; essentially no object exists to execute the method on. I think your best solution would be to find a way to determine if the service has stopped, if it has, restart it and then call ExecuteCommand. Otherwise, you could use kind of a "dirty hack" and attempt the call, if it fails, catch the exception and start the service and try a second time. By no means preferred, but possible.
EDIT:
Assuming that you're using a ServiceController you can use the Status property to determine if the service is running or not.
It's not possible to ExecuteCommands against a stopped service.
If it's not running, then it's not "there" to listen for the commands you send it.