How can I automate / schedule a WCF service call hosted in IIS?
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 e开发者_如何转开发xecute 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.
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.
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
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
精彩评论