2009-02-10 65 views
1

我刚刚开始修补WCF REST入门工具包,我看了屏幕录像,并且......我怀疑我只是在愚蠢。 :)开箱即用(使用提供的模板)我可以为单例资源或资源集合创建REST服务。但是在同一个项目中支持不同种类的资源呢?例如,如果我有书籍,我也想拥有作者和发布者。使用提供的模板我没有看到一个简单的方法来实现这一点。为每个资源创建一个服务(以及VS项目)听起来很荒谬。我需要在单个服务中支持多种资源类型,以便可以在类似的URL下访问它们,以便用户只需要替换最后一部分,如http://service/books即可获取所有书籍,并且http://service/authors/32可以获得特定作者。WCF REST入门套件 - 支持多种资源?

任何人都可以对此有所了解吗?我知道这可以使用普通的WCF服务创建,但入门工具包已经包含了所有的样板,那么为什么不使用它呢?如何处理模板生成的项目以添加对不同资源类型的支持?

谢谢。

回答

2

我想你的WCF REST入门套件。尝试将WCF REST入门工具包看作只是一个配置为在http环境中轻松设置的WCF服务。为WCF REST入门工具包设置的默认模板旨在用作示例。您将不得不创建自己的签名或使其符合您的需求。

您需要关注的关键部分是.svc文件中的代码(无法双击它,您必须选择打开方式)和[ServiceContract]接口。

在提供的代码中修改[ServiceContract]接口,看起来就像是对于常规的WCF服务。

这是您的需要修改

[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()); 
    } 
} 
+0

的Atom发布频道我想通了这一点,最终的样本。我试图将模板项目作为我的开发基础,而不仅仅是样本,它们是。我刚刚创建了自己的WCF服务,并重新使用Starter Kit中的扩展库。 – 2009-02-16 14:17:54