Well maybe not the simplest…but very very simple.

From time to time I need to do some testing against an OData service that is simple and implements basic functionality.  Sometimes I want this service to be accessible on the web, sometimes I just want it to be private to my VM.  For these scenarios I created a simple service that produces 10 random numbers along with unique IDs.

For those of you not familiar with developing OData services, it is very easy using Visual Studio and .NET.

Attached is a simple Visual Studio project that implements the most basic OData service using WCF Data Services.  Take a look, and you will see that WCF handles all of the plumbing, all you need to do is provide the data and the properties/methods…then publish to Azure web sites or host it yourself.

Here is a listing of the code…

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace RandomNumbers
{
    public class RandomNumbers : DataService<RandomNumbersDataSource>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // set rules to indicate which entity sets and service operations are visible, updatable, etc.
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            //config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }

    [DataServiceKey("ID")]
    public class RandomNumber
    {
        private RandomNumber()
        {
        }

        public RandomNumber(int id, int number)
        {
            this.ID = id;
            this.Number = number;
        }

        public int ID
        {
            get;
            private set;
        }

        public int Number
        {
            get;
            private set;
        }
    }

    public class RandomNumbersDataSource
    {
        private static Random rnd = new Random(DateTime.Now.Millisecond);

        private static List<RandomNumber> numbers = new List<RandomNumber>
        {
            new RandomNumber(0, rnd.Next(0,100)),
            new RandomNumber(1, rnd.Next(0,100)),
            new RandomNumber(2, rnd.Next(0,100)),
            new RandomNumber(3, rnd.Next(0,100)),
            new RandomNumber(4, rnd.Next(0,100)),
            new RandomNumber(5, rnd.Next(0,100)),
            new RandomNumber(6, rnd.Next(0,100)),
            new RandomNumber(7, rnd.Next(0,100)),
            new RandomNumber(8, rnd.Next(0,100)),
            new RandomNumber(9, rnd.Next(0,100))
        };

        public RandomNumbersDataSource()
        {
        }

        public IQueryable<RandomNumber> RandomNumbers
        {
            get
            {
                return numbers.AsQueryable();
            }
        }
    }
}