Introduction
ServiceStack is a framework similar to ASP.NET MVC, Web API, WCF etc. used to build web applications and services. The framework is platform independent and the applications built using it can be run on both Windows and Linux. In this article we will build a simple REST based web service that will return ‘Note’ details associated with a given ‘Id’. When the ‘Id’ parameter is not specified in the request all the ‘Notes’ are returned to the client.
Pre-requisites
Following are the pre-requisites:
- Visual Studio 2015.
- ‘ServiceStack’ NuGet Package.
Here are the steps to create the base project:
- Create a new project using the ‘Empty’ ASP.Net application project template.
- Add ‘Global.asax’ file to the project.
- Install the ‘ServiceStack’ NuGet package in this project.
Request and Response
The ServiceStack framework uses the Data Transfer Objects design pattern. This requires defining different classes for the Request and Response messages for a given Service that are independent of the domain specific classes that are used inside the Service. We will create two classes for this.
- Add request and response DTO classes
- [Route("/notes")]
- [Route("/notes/{Id}")]
- class NoteRequestDTO
- {
- public int? Id { get; set; }
- }
- class NoteResponseDTO
- {
- public string Note { get; set; }
- }
- [Route("/notes")]: This defines the URL for retrieving all the Notes.
- [Route("/notes/{Id}")]: This defines the URL for retrieving a specific Note.
- namespace NotesServer
- {
- class NotesService : Service
- {
- //Collection of Notes
- private Dictionary<int, string> notes = new Dictionary<int, string>() {
- { 1, "Hello" },
- { 2, "World" } };
- public object Any(NoteRequestDTO request)
- {
- //Check if the Note Id was passed in the Request.
- if (!request.Id.HasValue)
- {
- //Note Id is not present in the Request, hence return all Notes.
- var notesResponse = from note in notes
- select new NoteResponseDTO {
- Note = note.Value
- };
- return notesResponse.ToList();
- }
- //Note Id is present in the Request. Check if there is a corresponding Note.
- if (notes.ContainsKey(request.Id.Value))
- {
- //Return the corresponding Note.
- return new NoteResponseDTO { Note = notes[request.Id.Value] };
- }
- else
- {
- //Return Empty string as there is no corresponding Note.
- return string.Empty;
- }
- }
- }
- }
Add the following two classes that represent the input and output DTO objects of the NotesService.
The NoteRequestDTO class is used to define the URL Route used to invoke the service. Here we have defined two routes.
NotesService
The NotesService class defines the implementation of a service that returns Notes. This class should extend the ‘Service’ class of ServiceStack. The class defines only one method ‘Any’ which will Handle all the HTTP method types, viz., GET, POST, PUT and DELETE. This method takes the Request DTO class defined earlier as a parameter. The return type varies based on the input. The service has a predefined list of Notes. ServiceStack framework will parse the input and fills the Request DTO object. When the Id parameter is not passed we need to return the whole Notes collection. If the specific Id is passed, we look up and return the corresponding Note. If there is no Note found corresponding to the Id then we just return an empty string.




Ankit BansalPosted Sep 28, 2015, 12:52 AM
nice one..thanks ..
Harshad PansuriyaPosted Sep 28, 2015, 12:37 AM
Nice One Sir
Santhakumar MunuswamyPosted Sep 27, 2015, 3:03 AM
Good Article
RakeshPosted Sep 27, 2015, 12:33 AM
Good share sir