IgorShare Thoughts and Ideas

Consulting and Training

Exposing dynamic data via RSS feed

Posted by Igor Moochnick on 02/11/2008

In my recent article “RSS is not only for the news” I’ve mentioned that the current Syndication API is pretty flexible and allows a great deal of control of what is sent over the wire. This allows you to give the client almost anything it asks for. For example your application can provide a graphical information, a binary data, etc… Think about all the different way you can present your data.

Here is a simple example: let’s say you have a resource that is changing over the time (it can be traffic data). You can provide a link to this data and as soon as a user (or another service) navigates there – the data (for example, XML) can be streamed as a response. But what if it’s a real user and you want to be friendly and present this data in a graphical form (render the graph)? You can do that by just adding additional parameter to the URL that will tell your service what format the data can be (should be) presented in.

Remember the feed contract?

[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
[ServiceContract]
public interface IFileChangeFeed
{
    // ... other operations

    [OperationContract]
    [WebGet(UriTemplate = "file?path={path}")]
    Stream GetFile(string path);
}

Note that the GetFile request has a URL parameter. In my case the parameter tells the service what file to retrieve, but in your case it can tell your service what data to generate and in what format. [Note: please don’t deploy this example on the real systems until you know EXACTLY how and who is accessing your feed. The sample, deployed AS-IS, exposes your system to the external attacks]

The URL request that will be routed to the GetFile method can look like this: http://yourHost/service/location/file?path=fileName.ext

The WCF will find a match in your contract and will call the GetFile method, substituting the “path” variable with the value of the “path” parameter.

public Stream GetFile(string path)
{
    if (File.Exists(path))
    {
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
        WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.ContentLength, 
             new FileInfo(path).Length.ToString());
        return new FileStream(path, FileMode.Open, FileAccess.Read);
    }
    else
    {
        WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
        return null;
    }
}

 

This is a pretty straightforward way to return back any type of data, depending on the incoming request. As you can see it’s extremely simple, so unravel your imagination and let REST be your friend!

BTW: Just found an MSDN article, HTTP Programming with WCF and the .NET Framework 3.5,  talks about similar issues and guides you through other REST-related examples.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: