3

I've got an extraordinarily simple service contract for a service that manages its own data import, similar to this:

using System.ServiceModel;

namespace Company.Services.Domain.Contract
{
    [ServiceContract(Name = "ImportService", Namespace = "http://services.company.org/")]
    public interface IImportService
    {
        [OperationContract]
        ImportResponse ImportData();
    }
}

I'd like to set up a scheduled task or something similar to execute this call on a daily basis. I know that I could write a console application and create a proxy via svcutil. But is there a way to configure this in IIS natively (maybe an IIS extension)? Or could I achieve something simple and elegant with PowerShell?

Just wondering what my options are.

Brandon Linton
  • 4,373
  • 5
  • 42
  • 63

3 Answers3

4

You can have an IIS-hosted application that uses IIS auto-start, assuming you are running at least version 7.5 of IIS. The auto-start does not run on a timer, but will help guarantee that your application is always running. After IIS starts the application, your code can start a timer to call the WCF service each day.

The timer would, of course, be implemented in code. Examples of simple timer implementations include this, this, and this.

If you are using ASP.NET then put the timer start code in the Application_Start method in the global.asax file of the auto-started application.

Community
  • 1
  • 1
schellack
  • 10,144
  • 1
  • 29
  • 33
  • Auto-start is a sweet feature for keeping the service highly available...not sure how it would call a specific service method at a given time of day though. App start would only give it to me once as it fires up, right? – Brandon Linton Aug 01 '11 at 17:07
  • You would have a recurring timer in your code that is started by App Start. the IIS auto-start would ensure that it's always running. I edited my answer to include links to some sample timer implementations in a few lines of code. – schellack Aug 02 '11 at 14:32
3

You could use cURL (Windows and 'nix versions available): http://curl.haxx.se/

Set your scheduled task to invoke curl.exe with parameters appropriate to make a request to your web service's URL:

curl -o importResponse.txt http://services.company.org/ImportService.svc/ImportData

The -o will drop the response into a text file of the given name. Other configuration options can be found in the manual: http://curl.haxx.se/docs/manual.html

EDIT: This assumes that you're exposing a web service and GET (rather than POST or SOAP). If you're exposing a SOAP service it should still be fairly trivial because you're not passing any parameters.

curl --request POST --header 'Content-type: text/xml' --output importResponse.txt --data <SOAP/> http://services.company.org/ImportService.svc/ImportData

Alternatively, as you're using WCF you can expose your service as both a SOAP service and a RESTful web service simultaneously, by adding some configuration to your app's web.config: a webHttpBinding, a service endpoint, and an endpointBehavior, as per: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx

Ash Eldritch
  • 1,504
  • 10
  • 13
  • Cool utility! Wouldn't I have to snag the SOAP body though and post that as a parameter? – Brandon Linton Aug 01 '11 at 17:05
  • Ah, yes, if you're using SOAP it's a little less simple. I've updated the answer appropriately. – Ash Eldritch Aug 02 '11 at 00:48
  • I attempted the updated curl line but it didn't appear to do the trick...I'm sure I could find the SOAP generated in a normal call and plug it in but I'll have to play with it some more. Going to see if that RESTful way works as well. – Brandon Linton Aug 02 '11 at 20:41
  • Making the method invokable through a GET will make it extraordinarily simple to automate. Just verified it working thanks to that blog entry. – Brandon Linton Aug 03 '11 at 18:14
3

Check ou New-WebServiceProxy. It's very useful, you point it at the url and it loads the web ervice. You can pipe it to Get-Member to see whats available as well.

Matt

EDIT: example now I'm not on my phone :-)

# url for the service
$url = "http://services.company.org/ImportService"             

# Create the service - using default credentials         
$service = New-WebServiceProxy $Url -UseDefaultCredentials

# explore the service
$service | gm

for more information check out the PowerShell help

help New-WebServiceProxy
Matt
  • 1,931
  • 12
  • 20