Syndication
By publishing the service results in either RSS 2.0 or Atom 1.0 format, your customers get automatic notifications whenever there is some change in the output.
- Create a new WCF Service Library and name it as ‘TemperatureServiceLibrary’.
- Rename the default service interface and service implementation files as ‘ITemperatureService.cs’ and ‘TemperatureService.cs’.
- Open ‘ITemperatureService.cs’ and modify the code to look like the one below.
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;
namespace TemperatureServiceLibrary
{
[ServiceContract]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
public interface ITemperatureService
{
[OperationContract]
[WebGet(
UriTemplate = "GetForecast/{format}")]
SyndicationFeedFormatter GetForecast(string format);
//Atom10FeedFormatter GetForecast();
//Rss20FeedFormatter GetForecast();
}
[DataContract(Name = "Forecast", Namespace = "")]
public class Forecast
{
[DataMember(Name = "City")]
public string City { get; set; }
[DataMember(Name = "Day1LowTemp")]
public double Day1LowTemp { get; set; }
[DataMember(Name = "Day1HighTemp")]
public int Day1HighTemp { get; set; }
[DataMember(Name = "Day1Details")]
public string Day1Details { get; set; }
[DataMember(Name = "Day2LowTemp")]
public int Day2LowTemp { get; set; }
[DataMember(Name = "Day2HighTemp")]
public int Day2HighTemp { get; set; }
[DataMember(Name = "Day2Details")]
public string Day2Details { get; set; }
}
}
- Put the following code into TemperatureService.cs
using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
namespace TemperatureServiceLibrary
{
public class TemperatureService : ITemperatureService
{
#region ITemperatureService Members
//public Rss20FeedFormatter GetForecast() //–Use this signature if you just want to emit RSS 2.0
//public Atom10FeedFormatter GetForecast() //–Use this signature if you just want to emit Atom 1.0
public SyndicationFeedFormatter GetForecast(string format)
{
var feed = new SyndicationFeed();
feed.Title = new TextSyndicationContent(
“Eden Prairie Weather Forecast”);
feed.Authors.Add(new SyndicationPerson(
“weather@appdev.com”));
feed.Categories.Add(
new SyndicationCategory(“Weather Forecasts”));
feed.Description = new TextSyndicationContent(
“This is the weather forecast for Eden Prairie”);
var randomNumber = new Random();
var items = new List<SyndicationItem>();
var item = new SyndicationItem(“Day 1 Forecast”,
String.Format(“Cold, windy, snowy. ” +
“Low of {0}. High of {1}.”,
randomNumber.Next(20, 30).ToString(),
randomNumber.Next(30, 40).ToString()),
new Uri(“http://www.mysite.com”),
“Today”, DateTime.Now);
items.Add(item);
item = new SyndicationItem(“Day 2 Forecast”,
String.Format(“Colder, windier, snowier. ” +
“Low of {0}. High of {1}.”,
randomNumber.Next(10, 20).ToString(),
randomNumber.Next(20, 30).ToString()),
new Uri(“http://www.mysite.com”),
“Tomorrow”, DateTime.Now);
items.Add(item);
item = new SyndicationItem(“Day 3 Forecast”,
String.Format(“blah. blah. blah. ” +
“Low of {0}. High of {1}.”,
randomNumber.Next(15, 25).ToString(),
randomNumber.Next(30, 40).ToString()),
new Uri(“http://www.envirnment.com”),
“Day After”, DateTime.Now);
items.Add(item);
feed.Items = items;
//return new Rss20FeedFormatter(feed); //–Uncomment this to emit RSS 2.0
//return new Atom10FeedFormatter(feed); //–Uncomment this to emit Atom 1.0
//Begin commenting here if you are emitting specific syndication standard
if (format == “rss”)
{
return new Rss20FeedFormatter(feed);
}
else if (format == “atom”)
{
return new Atom10FeedFormatter(feed);
}
else
{
return null;
}
//End of comments for specific syndication standard
}
#endregion
}
}
- Create a web site as the host for the TemperatureServiceLibrary. Remove the default IService.cs and Service.cs files and add project reference to the TemperatureServiceLibrary project.
- Rename the ‘Service.svc’ to ‘TemperatureService.svc’ and change the ServiceHost directive to look like this.
<%@ ServiceHost Service=”TemperatureServiceLibrary.TemperatureService”
Factory=”System.ServiceModel.Activation.WebServiceHostFactory” %>
- Change the binding in the Web.config file to ‘webHttpBinding’.
- Browse the SVC file. You’ll get an error message, which is expected. Try with the following URL (the port number may be different in your environment).
http://localhost:2263/WebHost/TemperatureService.svc/getforecast/atom
You should see something like this:
Right click on the page and select ‘View Source’. You should see the service result in Atom 1.0 format, like this:
<feed xmlns=”http://www.w3.org/2005/Atom”>
<title type=”text”>Eden Prairie Weather Forecast</title>
<subtitle type=”text”>This is the weather forecast for Eden Prairie</subtitle>
<id>uuid:0a92f15e-1aa7-455d-84ab-261dcd0eac74;id=1</id>
<updated>2010-06-20T18:26:29Z</updated>
<category term=”Weather Forecasts”/>
<author>
<email>weather@appdev.com</email>
</author>
<entry>
<id>Today</id>
<title type=”text”>Day 1 Forecast</title>
<updated>2010-06-20T23:56:29+05:30</updated>
<link rel=”alternate” href=”http://www.mysite.com/”/>
<content type=”text”>Cold, windy, snowy. Low of 20. High of 32.</content>
</entry>
<entry>
<id>Tomorrow</id>
<title type=”text”>Day 2 Forecast</title>
<updated>2010-06-20T23:56:29+05:30</updated>
<link rel=”alternate” href=”http://www.mysite.com/”/>
<content type=”text”>Colder, windier, snowier. Low of 15. High of 20.</content>
</entry>
<entry>
<id>Day After</id>
<title type=”text”>Day 3 Forecast</title>
<updated>2010-06-20T23:56:29+05:30</updated>
<link rel=”alternate” href=”http://www.envirnment.com/”/>
<content type=”text”>blah. blah. blah. Low of 20. High of 31.</content>
</entry>
</feed>
- Try the following URL to get the same output, but in RSS 2.0 format.
http://localhost:2263/WebHost/TemperatureService.svc/getforecast/atom
By
Team CVK
Microsoft Competency
www.cvktech.com