I've created a WCF service application containing a client, contract and a service. I use the client to call methods implemented in the service.
Is there a way to have a timer in the service and to set its interval to tick and to have a method that is executing during the time the service is being called.
I tried to create a constructor to the service (static and non-static) but it didn't work.
I thought about creating a 3rd party client, which will execute a method implemented in the service which will do what I need.
Thanks,
Oded.
If you want something running while the service is called, then you could do it from the client:
Call start process from the client
Call the service from the client
Call stop process from the client
You could also do it on the server side:
Your first line in the service is start process
Your last line in the service is stop process
Note you must be carefull about error handling, otherwise you will end up with many orphaned processes.
Related
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
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.
I'm trying to migrate a .NEt Remoting app to WCF, right now both services(Remoting and WCF) are living together.
I have one client consuming both of this services at the same time and I noticed that in the very first call to WCF service it takes a little bit longer than the first call to .NET Remoting service.
With .NET Remoting service the first call get response almost immediately.
.NEt Remoting first call response time: less than a second.
WCF first call response time: about 2 seconds.
I know that there is some initialization cost for WCF connection to be opened, but how can I accelerate this WCF channel wakeup time ??
Any hint?
WCF is doing so much more than remoting. There is a cost for this work. Full stop.
Try calling the WCF service before you need it. Consider adding a Heartbeat() or Init() method to the service to trigger the startup process. If the startup is completed by the first call, there should not be a delay for subsequent calls.
If you have the option to host in AppFabric then you could use the Auto-Start Feature which is specifically designed to get the application initialized before the first client call.
Benefits of the Auto-Start Feature
When you enable the auto-start
feature for a service, the service is up and running as soon as the
application that it belongs to is started and before the service
receives the first WCF message from the client. Therefore, the service
processes the first message quickly because it is already initialized.
I have a windows service that creates an instance of UdpClient class and listen for incoming request. I have a helper class to run it as a console application. When i run it as a console program, it runs fine and receive connections normally.
When deployed as a Windows Service using "installutil MyService" and start it, I get the following error:
"Error 1053: the service did not respond to the start or control request in a timely fashion."
The service don't even start, so I can't place a breakpoint in code.
Any suggestion about how to debug this or where to look?
Thanks.
Your Start() method is taking too long to do whatever it does when the service starts - if memory serves you have something like 15 seconds to kick-off whatever tasks the service is supposed to perform (on separate threads for long-running or continuous activities) and you must then allow the startup method to end so that the Service Controller recognises that the service has finished starting.
You didn't post any code, but my assumption would be that you're listening for UDP traffic synchronously - so the startup method just sits there waiting for UDP messages and never ends. Put the UDP listener into its own thread, start that thread, and then let the startup method end.
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.