tags:

views:

809

answers:

1

I just started tinkering with the WCF REST Starter Kit, I watched the screencasts and... I suspect I'm just being stupid. :) Out of the box (with the provided templates) I can create REST services for a singleton resource or a collection of a resource. But what about support for different kinds of resources in the same project? In example if I have Books, I also want to have Authors and Publishers. With the provided templates I don't see an easy way to accomplish this. Creating a service (and thus a VS project) for each resource sounds ridiculous to me. I need to support multiple resource types in a single service so that they can be accessed under a similar URL, so that the user has only to replace the last part, like http://service/books to get all books and http://service/authors/32 to get a particular author.

Can anyone shed some light on this? I know this can be created with a plain WCF service, but the Starter Kit has all the boilerplate in place already, so why not use it? How would one approach a template generated project to add support for different resource types?

Thanks.

+2  A: 

I think your overthinking the WCF REST Starter Kit. Try to think of the WCF REST Starter Kit as just a WCF Service that's configured for easy setup in the http environment. The default templates that are setup for the WCF REST Starter Kit are meant to be used as a sample. You will have to just create your own signature or adapt theirs to meet your needs.

The key parts that you'll want to focus on are the code in the .svc file (you can't access it double clicking, you'll have to choose open with) and the [ServiceContract] interfaces.

Modify the [ServiceContract] interface in the code behind provided to look just like it would for a regular WCF Service.

Here is a sample of an ATOM Pub Feed modified for your needs

[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract]
public partial class LibraryFeed
{
    public LibraryFeed()
    {
    }

    /// <summary>
    /// Returns an Atom feed.
    /// </summary>
    /// <returns>Atom feed in response to a HTTP GET request at URLs conforming to the URI template of the WebGetAttribute.</returns>
    [WebHelp(Comment = "Gets a list of Books.")]
    [WebGet(UriTemplate = "/books/?numItems={numItems}")]
    [OperationContract]
    public Atom10FeedFormatter GetBooks(int numItems)
    {
        var books = GetBooks();
        List<SyndicationItem> items = GetItems(books, numItems);

        SyndicationFeed feed = CreateFeed(items);

        WebOperationContext.Current.OutgoingResponse.ContentType = ContentTypes.Atom;
        return feed.GetAtom10Formatter();
    }

    /// <summary>
    /// Returns an Atom feed.
    /// </summary>
    /// <returns>Atom feed in response to a HTTP GET request at URLs conforming to the URI template of the WebGetAttribute.</returns>
    [WebHelp(Comment = "Gets a single author.")]
    [WebGet(UriTemplate = "/authors/?numItems={numItems}")]
    [OperationContract]
    public Atom10FeedFormatter GetAuthors(int numItems)
    {
        var authors = GetAuthors();
        List<SyndicationItem> items = GetItems(authors, numItems);

        SyndicationFeed feed = CreateFeed(items);

        WebOperationContext.Current.OutgoingResponse.ContentType = ContentTypes.Atom;
        return feed.GetAtom10Formatter();
    }

    /// <summary>
    /// Returns an Atom feed.
    /// </summary>
    /// <returns>Atom feed in response to a HTTP GET request at URLs conforming to the URI template of the WebGetAttribute.</returns>
    [WebHelp(Comment = "Gets a list of Authors.")]
    [WebGet(UriTemplate = "/authors/{id}")]
    [OperationContract]
    public Atom10FeedFormatter GetAuthor(string id)
    {
        var author = GetSingleAuthor(id);
        WebOperationContext.Current.OutgoingResponse.ContentType = ContentTypes.Atom;
        return GetItem(author.GetAtom10Formatter());
    }
}
bendewey
I figured that out eventually. I was trying to use the template projects as the base of my development, not merely samples, which they are. I just created my own WCF Service and am reusing the extensions library from the Starter Kit.
Pawel Krakowiak