it:ad:wcf:data_services:odata

IT:AD:WCF Data Services:OData

  • Add/New Item/WCF Data Service
    • formerly known as ADO.NET Data Services or “Astoria”
  • The absolute minimum to get it up and running is add a Class to the Generic Service.

Example:

namespace StackOveflow
{
    //Min required step, add Entity.
    //Optionally make code available via JSONP (see: http://bit.ly/LzqfYj) as well as default JSON
    [JSONPSupportBehavior]
    public class Service : DataService<StackOverflowEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            //Check up:
            //config.SetResourceContainerAccessRule("Customers", ResourceContainerRights.AllRead);
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
             
            //This could be "*" and could also be ReadSingle, etc, etc.
            config.SetServiceOperationAccessRule("GetPopularPosts", ServiceOperationRights.AllRead);
             
            //Set a reasonable paging site
            config.SetEntitySetPageSize("*", 25);
             
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
 
        protected override void OnStartProcessingRequest(ProcessRequestArgs args)
        {
            base.OnStartProcessingRequest(args);
            //Cache for a minute based on querystring
            HttpContext context = HttpContext.Current;
            HttpCachePolicy c = HttpContext.Current.Response.Cache;
            c.SetCacheability(HttpCacheability.ServerAndPrivate);
            c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
            c.VaryByHeaders["Accept"] = true;
            c.VaryByHeaders["Accept-Charset"] = true;
            c.VaryByHeaders["Accept-Encoding"] = true;
            c.VaryByParams["*"] = true;
        }
 
        //Create a method that returns IQueryable. 
        //Make it GETable.
        [WebGet]
        public IQueryable<Post> GetPopularPosts()
        {
            var popularPosts = (from p in this.CurrentDataSource.Posts 
                               orderby p.ViewCount
                               select p).Take(20);
 
            return popularPosts;
        }
    }
}

Read's Scott's article.

  • /home/skysigal/public_html/data/pages/it/ad/wcf/data_services/odata.txt
  • Last modified: 2023/11/04 02:01
  • by 127.0.0.1