Introducing Lullaby

written by Ryan Olshan on Wednesday, September 10 2008

Lullaby is an open source attribute-based REST API for building REST services on the .NET platform. There is an early beta available for download at http://code.google.com/p/ryanolshanrest. Portions of the code are based on REST.NET (http://code.google.com/restdotnet).

Creating a REST Enabled Service

Create an web handler that inherits from Lullaby.RestHandler and register the web handler in Web.config.

    public class RestServices : RestHandler

    {

    }

 

<add verb="*" path="RestServices.ashx" type="RestTest.RestServices, RestTest" />

Add the RestAssemblyAttribute to the assembly. By default, only assemblies in the websites bin folder with this attribute are searched.

[assembly: RestAssembly]

Create a class using the RestClassAttribute and RestMethodAttribute attributes. On the RestMethodAttribute, set the URL template and HTTP method.

    [RestClass("User")]

    public class User

    {

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int UserID { get; set; }

 

        [RestMethod("/{userid}", HttpMethods.Method.GET)]

        public User GetUser(int userid)

        {

            return new User() { FirstName = "Bob", LastName = "Smith", UserID = userid };

        }

    }

The GET method on the above example would be accessable via http://domain.com/RestHandler.ashx/User/12, for example, if you were passing in a userid of 12.

[RestClass]

If no value is supplied to the constructor of the RestClassAttribute, it will use the name of the class as a default value and as such the above example would be accessable via http://domain.com/RestHandler.ashx/User/12, for example, if you were passing in a userid of 12.

To specify that no prefix should be generated for the REST class from the RestClassAttribute, pass false into the constructor.

[RestClass(false)]

Now the GET method will be accessable via http://domain.com/RestHandler.ashx/12, for example, if you were passing in a userid of 12.

The following settings are available for Lullaby's custom configuration section.

<?xml version="1.0"?>

<configuration>

  <configSections>

    <section name="restSettings" type="Lullaby.ConfigurationHandler.RestConfigurationSection, Lullaby" />

  </configSections>

 

  <restSettings>

    <caching cacheDuration="60" providerType="Lullaby.Caching.FileCacheProvider, Lullaby" fileCacheProviderFileLocation="C:\LullabyCache" />

    <serialization defaultProviderType="Lullaby.Serializers.XmlSerializer, Lullaby" />

    <restAssemblies>

      <add assembly="MyRestAssembly1" />

      <add assembly="MyRestAssembly2" />

    </restAssemblies>

    <restAssemblyDirectories>

      <add directory="c:\MyDirectory1" searchSubFolders="false" />

      <add directory="c:\MyDirectory2" searchSubFolders="true" />

    </restAssemblyDirectories>

  </restSettings>

</configuration>

caching - Element for the caching provider that Lullaby uses. Lullaby has two built in cache providers:

  • Lullaby.Caching.FileCacheProvider - Provider for caching to disk. When using this provider, you need to specify the cache directory using fileCacheProviderFileLocation.
  • Lullaby.Caching.MemoryCacheProvider - Provider for caching to memory

To create a cache provider, extend Lullaby.Caching.CacheProviderBase.

serialization - Element for setting the default serializer that Lullaby uses if it's unable to deduce a request type. Lullaby has two built in serializers:

  • Lullaby.Serializers.JsonSerializer
  • Lullaby.Serializers.XmlSerializer

To create an XML serialization provider, implement the interface Lullaby.Serializers.ISerializationProvider.

restAssemblies - Element for specifying additional assemblies to search. This allows Lullaby to search assemblies not marked with the RestAssemblyAttribute and assemblies in the GAC.

restAssebmlyFolders - Element for specifying additional folders for Lullaby to search for assemblies in. To search sub folders within the folder, set searchSubFolders to true.

That's Lullaby in a nutshell. If you have any bugs or feedback, you can contact me at ryan [at] ryanolshan [dot] com.

Kick this post on .NET Kicks

Similar Posts

  1. Lullaby's authentication provider framework
  2. Announcing Lullaby 1.0 RC1
  3. Handling POST and PUT methods with Lullaby

Post a comment