代码之家  ›  专栏  ›  技术社区  ›  Pawel Krakowiak

WCF REST初学者工具包-支持多种资源?

  •  1
  • Pawel Krakowiak  · 技术社区  · 16 年前

    我刚开始修补WCF REST初学者工具包,我看了屏幕广播,然后。。。我怀疑我只是太愚蠢了开箱即用(使用提供的模板),我可以为单例资源或资源集合创建REST服务。但是,对于同一项目中不同类型资源的支持又如何呢?例如,如果我有书,我也希望有作者和出版商。使用提供的模板,我看不到一个简单的方法来实现这一点。对我来说,为每个资源创建一个服务(从而创建一个VS项目)听起来很可笑。我需要在单个服务中支持多个资源类型,以便可以在类似的URL下访问它们,这样用户只需替换最后一部分,如 http://service/books 得到所有的书和 http://service/authors/32

    有人能解释一下吗?我知道这可以通过简单的WCF服务创建,但是初学者工具包已经准备好了所有的样板文件,所以为什么不使用它呢?如何处理模板生成的项目以添加对不同资源类型的支持?

    1 回复  |  直到 16 年前
        1
  •  2
  •   bendewey    16 年前

    我认为你对WCF REST初学者工具包的过度思考。试着将WCF REST Starter工具包看作是一个WCF服务,它的配置便于在http环境中进行设置。为WCF REST初学者工具包设置的默认模板将用作示例。你只需创建自己的签名或修改他们的签名以满足你的需要。

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

    在提供的代码隐藏中修改[ServiceContract]接口,使其看起来与常规WCF服务一样。

    下面是一个根据您的需要修改的ATOM发布提要示例

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