6

I have below requirements:
(1) Perform 'action A' when user requests for it.
(2) We also want to perform the same 'Action A' twice in a day even if users don't request for it.

I have a WCF web service which has method XYZ that performs action A. The method XYZ will be called when user requests for it.

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

Is there a way to perform an action by user request and schedule that same action, using only one application?

hari
  • 9,439
  • 27
  • 76
  • 110
sharp_net
  • 737
  • 2
  • 5
  • 12

2 Answers2

6

No, WCF cannot be auto-scheduled. You need to implement a Scheduled Task (see Scheduling jobs on windows), a Windows Service with a timer (which you've said you don't want to do, if I understand correctly) or some other application with a timer.

You could start a thread as per the other answer but this relies on your service calling itself - I'd prefer to call it externally, from another process.

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

I normally prefer to implement this type of timer through a Windows Service, simply because the Windows Service can be monitored, can log, and can auto-start / auto-restart - install it and it 'just works'. If I didn't want to use a Windows Service then I'd schedule a task.

Community
  • 1
  • 1
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
4

I typically do this by just calling the WCF service method from some kind of task scheduler. In a really simple form, you could just spawn a Thread from your service, that runs the WCF method periodically. Again this isnt the best solution, but its easiest to demonstrate. You could use some other scheduler library to do this too...

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

Then somewhere in the application startup:

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 it, although not the best way, but basically you can just call the WCF service method just like any other method in the application.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138