ServiceStack - An Introduction

What is a ServiceStack?

ServiceStack is an open source, cross-platform for .Net and Mono REST web service framework and can run on both .Net framework as well in Mono platform too. ServiceStack have the flexibility of auto configuration of all standard output formats – XML, JSON, CSV, JSV, SOAP1.1, SOAP1.2 and endpoints –REST, RPC, SOAP without any configuration. It embraces only the concepts of DTO and not RPC concept. Whereas, WCF has the option of both. Service developed in service stack can run in windows environment using .Net framework or in Linux OS with Mono* support.

*Mono supported OS are:

Linux, Mac OS X, iOS, Sun Solaris, BSD, MS Windows, Nintendo Wii, Sony PlayStation 3 etc.


Why ServiceStack?

ServiceStack is not instead of WCF for its better performance, but main feature is that it can be deployed and works in cross platform like Windows, Mono, Linux, iOS, Mac OS etc.


ServiceStack versus WCF:

- WCF can be deployed only windows platform whereas Service Stack supports cross OS environment deployment.
- WCF works on both DTO and RPC concepts whereas Service Stack works uses DTO concept only.
- In WCF, if we do not define attribute [DataContract] then it will ignore it. But Service Stack will serialize it in plain text.


Hosting a Service Stack: 
Service Stack can be hosted in any of the following:

  1. Stand-alone mode
  2. With an existing ASP.Net  web application
  3. Windows service
  4. Console application
For more detail, please read here.


Creating First Service Stack Web Service:

Download Service Stack libraries from visual studio package manager. To download run command:

Install-package ServiceStack

Steps to create a service stack:

Common name space:

using System.Web.UI;
using ServiceStack;
using ServiceStack.CacheAccess;
using ServiceStack.ServiceInterface;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.CacheAccess;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.OrmLite;
using ServiceStack.Common;

Step: 1 Create an empty web application and then install packages for service stack.

Step: 2 Add a class file ad define request and response class.

[Route("/HelloRequest")]
[Route("/HelloRequest/{Name}")]
public class HelloRequest
{
   public string Name { get; set; }
}

public class HelloResponce
{
   public string Result { get; set; }
}

public class HelloService : IService
{
   public object Any(EmployeeRequest reqObj)
   {
     return new HelloResponce { Result = "You have entered: " + reqObj.Name };
   }

  public object Get(EmployeeRequest reqObj)
  {
    Return your_definedtype;
  }
}


Step: 3 Add a class for AppHost.

public class HelloAppHost : AppHostBase
{
  public HelloAppHost() : base("Here is your service:", typeof(HelloService).Assembly)
 {

 }

  public override void Configure(Funq.Container container)
  {
    // Defined this route in class attribute
    Routes.Add("/HelloRequest")
   .Add("/HelloRequest/{Name}");
  }
}


Step: 4 Add a Global.asax file and add the following code line to initialize the service request.

protected void Application_Start(object sender, EventArgs e)
{
   new HelloAppHost().Init();
}


Step: 5 Add the following lines in Web.config file.


Step: 6 Build the service and run it. On browse you can see the following result, if everything is correct.






Step: 7 To test, type the URL as:

localhost:51391/hellorequest?name=navin&format=xml

This will display the result as you have passed in query string.

Comments

Popular posts from this blog

A new threat to users on “Incoming Calls” – VISHING

Session Storage - a client-side state management technique

HTTP Error 500.21 - Internal Server Error: ExtensionlessUrlHandler-ISAPI-4.0_32 bit has a bad module