How can I automate / schedule a WCF service call hosted in IIS? - wcf

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.

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

Related

Calling WCF service method from browser?

Since i am new to WCF and Having configured a WCF service endpoint in IIS at Virtual Directory Api ( url goes like
http://localhost/api/taskapi.svc)
i was looking for ways to make request through web browser something like
http://localhost/api/taskapi.svc/GetCompleted
would respond with returnd data .I know this requires the binding of web service with the webHttpBinding but i don't know how to do it any help would be great ?
Use WCFSVGHoST application to test WCF applications. Application enables you to key-in parameters value and execute method of your interest.
Link for the same:
http://msdn.microsoft.com/en-us/library/bb552363.aspx

call wcf web service from command line - no program

Is there a way to call a web method in WCF without ever writing an application or using cURL as suggested in WCF - possible to call a wcf service from the command line with parameters? ?
I was able to do this with asmx. For example, i was able to just launch in cmd:
> start http://myservice/abc.asmx/?op=DoSomething
but it doesn't seem to be possible on WCF web service.
Thanks
It depends on which binding you are using in WCF. The majority of the WCF bindings are SOAP-based services, which would require complex payloads to be produced and this wouldn't be feasible using CURL or the browser.
If however you used the WCF REST bindings (webHttpBinding) and had no requirements around HTTP header data you could call such as a service from the command line.
I would like to call out however WCF REST has since been superseded by ASP.NET WEB API as the preferred approach to creating REST-based services. If you have no other business usage for WCF then I would consider Web API as an alternative.

CXF Apache WSDL2Java set endpoint

In the beginning I have to warn that I'm not familar with web services, I want to simply generate what I need, and learn the basics of usage.
I recived .wsdl and .xsd files (stored localy). I have generated java code using Apache CXF WSDL2Java tool (I have generated a client). I also have an endpoint (as url without '?WSDL' on the end - whatever this endding means). How can I set this endpoint?
If I use:
Blachblach_Service ss = new Blachblach_Service(new URL(recived_url));
Blachblach port = ss.getBlachblachSOAP();
I get an exception. When I use soapUI to send XMLs to web services, everything works fine.
At first you need to initialize your web service client. See my answer over here how to make this work.
?WSDL ending means that you can see the web service WSDL file in your browser, you can access the web service through SOAP protocol by providing it with some valid request.
If you need to create your web service client using Spring. Here is very good example how to do this.
Yes usually we set params like end point URL on Service class object and retrieve port from it. and from port we invoke web service methods. can you please give details of exception you are getting?

WCF REST and asp.net and debugging

I have an asp.net 4 application that hosts a WCF REST service via WebServiceHost...
WebServiceHostFactory factory = new WebServiceHostFactory();
routes.Add(new ServiceRoute("mss", factory, typeof(ModuleStorage)));
My application has a custom authentication module and requires IIS Anonymous access. I also have Windows Integrated so that I can debug my application.
The issue I am having is the WebHttpBinding created by the WebServiceHost does not support having two authentication methods enabled in IIS. I can turn off Win. Auth. and it works but I can not debug.
So my question is... Can I enable WebHttpBinding to support both or can I somehow enable debugging without Win. Auth.
This service must be hosted as part of the application and I need a way to debug it.
Try debugging WCF REST Service using Fiddler, see the following links for more informations :
http://www.codeproject.com/Tips/213007/Debug-WCF-REST-Service
Depends what you are trying to debug. You could always trace the WCF service using SvcTraceViewer. Here is the config you can use on the server.
I ended up using ASP.NET Web API. I have to say so far I am a big fan!
http://www.asp.net/web-api

WCF - possible to call a wcf service from the command line with parameters?

Is it possible to call an IIS hosted wcf service from the command line and pass through parameters?
Yes. Write a console application that calls the WCF service, and then call the console application from the command line.
This will depend on what binding your WCF service uses but with cURL you can post pretty much everything to a given url.