3

如何使用 WCF 编写服务以返回 iCal?我看到的示例使用 xml 或 json 作为格式化响应的方式。返回其他类型的响应体的最佳方式是什么?

4

2 回答 2

3

像这样的东西:

[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...
}

因此,现在您可以对例如 yourService.svc/123.ics 执行 HTTP GET 并获取 iCal。

这样做的原因是“Stream”在 WCF REST 中是特殊的(用于非 XML、非 JSON 响应)。

请记住,您必须同时使用 WebHttpBinding 和 WebHttp 行为才能使其工作。

于 2009-10-05T23:17:26.310 回答
1

最简单的解决方案是以 XML 或 JSON 格式(您的选择)作为 WCF 调用中的简单字符串返回 iCal 表示:

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

然后继续在客户端上进一步处理它,一旦您收到包含 iCal XML(或 JSON)的字符串。这可以通过使用 SOAP 的标准 WCF 来完成。

其他方法可能是使用 WCF REST 服务,该服务在您点击特定 URL 时返回 iCal 格式的响应 - 这需要暂时安装WCF Rest Starter Kit (在 .NET 3.0/3.5 中)。我对 iCal 格式并不十分熟悉,但我确信通过某种方式,您将能够构造必要的 XML 格式来满足 iCal 要求。

马克

于 2009-10-05T19:26:36.157 回答