Can WCF be automatically scheduled?

I have below requirements:
(1) Perform action β€œA” when the user requests it.
(2) We also want to perform the same β€œaction A” twice a day, even if users do not request it.

I have a WCF web service that has an XYZ method that performs action A The XYZ method is called when the user requests it.

Now the question is, can I plan this action without creating a window service (which can host this service) or creating a proxy ?

Is there a way to perform an action at the request of the user and plan the same action using only one application?

+4
source share
2 answers

No, WCF cannot be scheduled automatically. You need to implement a scheduled task (see. Scheduling tasks in windows ), a Windows service with a timer (about which you said you did not want to do it if I understand correctly), or some other timer application.

You can start the thread according to another answer, but it depends on how you call your service - I would rather call it from the outside, from another process.

A scheduled task can run an executable file. You can write a console application that calls your WCF service, logs any result (if necessary), and then exits.

I usually prefer to implement this type of timer through the Windows service, simply because you can monitor the Windows service, log it in and automatically start / automatically restart - install it, and it "just works." If I do not want to use the Windows service, I plan a task.

+6
source

I usually do this by simply calling the WCF service method from a kind of task scheduler. In a really simple form, you can simply create a thread from your service, which periodically runs the WCF method. Again, this is not the best solution, but it is easiest to demonstrate. You can use another scheduler library to do this too ...

 [ServiceContract] public class SomeClass { [ServiceOperation] public void SomeServiceMethod() { ... } 

Then somewhere in the application launch:

 Thread t = new Thread(new ThreadStart(CallService)); t.Start(); ... // this will call the WCF service method once every hour public void CallService() { Thread.Sleep(3600000); // sleep 1 hour new SomeClass().SomeServiceMethod(); } 

This is one way to do this, although not the best way, but basically you can just call the WCF service method, like any other method in the application.

+4
source

Source: https://habr.com/ru/post/1369080/


All Articles