views:

124

answers:

2

How do I get my service written with WCF to return an iCal? The examples I see use either xml or json as the way to format the response. What is the best way to return other types of response bodies?

+1  A: 

The simplest solution would be to return the iCal representation in XML or JSON format (your pick) as a simple string from your WCF call:

[ServiceContract]
interface IMyCalService
{
  [OperationContract]
  string GetiCal(.......);
}

and then go on and handle it further on the client, once you've received the string containing the iCal XML (or JSON). This can be done with standard WCF using SOAP.

Other ways to do it might be to use a WCF REST service which returns a response in iCal format when you hit a particular URL - this would require installing the WCF Rest Starter Kit for the time being (in .NET 3.0/3.5). I'm not intimately familiar with the iCal format, but I'm sure one way or another, you'll be able to construct the necessary XML format to satisfy the iCal requirements.

Marc

marc_s
+1  A: 

Something like this:

[WebGet(UriTemplate="{id}.ics")]
[OperationContract]
Stream GetCalendar(int id)
{
   WebOperationContext.Current.OutgoingResponse.ContentType="text/calendar";

   //Now just return the appropriate data in iCal format in the Stream...
}

So now you can do an HTTP GET to e.g. yourService.svc/123.ics and get an iCal back.

The reason this works is that "Stream" is special-cased in WCF REST (used for non-XML, non-JSON responses).

Remember that you have to use both the WebHttpBinding and WebHttp behavior for this to work.

Eugene Osovetsky