joydip_kanjilal
Contributor

Working with the ServiceStack open source, cross platform Web service framework

opinion
Jul 22, 20155 mins
Software Development

Leverage ServiceStack to build cross platform high performance services with ease

ServiceStack is a good alternative to popular Microsoft technologies like WCF and WebAPI for building scalable web services because of its simplicity, high performance, true platform independence and less configuration. This article presents an overview of ServiceStack and how we can get started using it.

What is ServiceStack, anyway?

ServiceStack is an open source, simple, elegant, cross platform web service framework. You can build your Web services using ServiceStack and execute them either in Windows or in a Linux environment with Mono support. Because of its message based design, ServiceStack can easily provide support for all popular formats that include: JSON, XML, JSV, SOAP and also Protobuf. A message based design promotes extensible services. ServiceStack provides asynchronous support, i.e., you can call service methods asynchronously.

ServiceStack is highly testable and includes fast serializer which you can leverage for faster responses. You can use NuGet to include Protocol Buffers — a fast binary serializer from Google. ServiceStack also provides in-built support for typed validation and error handling. ServiceStack provides a rich integration with ASP.Net and ASP.Net MVC frameworks and it also includes a clean authentication and authorization provider model. ServiceStack is well documented — you can take a look at the documentation here.

Now that we have had a glimpse at the basics, let’s take a quick tour on how we can get started building a simple service using ServiceStack.

Getting Started

To create a service using ServiceStack, follow these steps:

  1. Create a new empty ASP.Net application project
  2. Save the project with a name
  3. Next, you should add the required ServiceStack references using NuGet

You should ensure that the following assemblies have been added to your project:

  • ServiceStack
  • ServiceStack.Client
  • ServiceStack.Common
  • ServiceStack.Interfaces
  • ServiceStack.Text

Once done, you should add the following lines to the web.config file of your application.

<system.webServer>

 <validation validateIntegratedModeConfiguration="false" />

<handlers>

<add path="*"

name="ServiceStack.Factory"

preCondition="integratedMode"

type="ServiceStack.HttpHandlerFactory, ServiceStack"

 verb="*" resourceType="Unspecified" allowPathInfo="true" />

</handlers>

</system.webServer>

Now you can create your service that leverages ServiceStack. Creating a service using ServiceStack is way too simple.

The next step is to create the request and response classes — these are standard C# classes anyway.

The following code snippet illustrates the request and response classes we would be using with our service.

The request class

namespace IDG

{

    public class IDGRequest

    {

        public string Address { get; set; }

    }

}

The response class

namespace IDG

{

    public class IDGResponse

    {

        public string Result { get; set; }

    }

}

The following code listing illustrates the service class. Note that your service class should extend ServiceStack.Service base class. Note how the request and response classes we created earlier have been used here.

using System;

using ServiceStack;

namespace IDG

{

    public class IDGService : Service

    {

        public object Post(IDGRequest request)

        {

            return new IDGResponse

            {

                Result = "Hello World"

            };

        }

    }

}

Now that you have created the service, the next step is to host it. To do this, you should create a class that extends the ServiceStack.AppHostBase class as shown in the code snippet given next.

using Funq;

using ServiceStack;

namespace IDG

{

    public class IDGAppHost : AppHostBase

    {

        public IDGAppHost():base("IDG Service Host", typeof(IDGService).Assembly)

        {

        }

        public override void Configure(Container container)

        {

            //You can specify your service configuration details here

        }

    }

}

Note that ServiceStack.AppHostBase is an abstract class — you should remmeber to override the Configure() method of this abstract base class in your inherited class and specify any configuration details as needed.

To bootstrap your service, you should make a call to the Init() method from the Application_Start event of the Global.asax.cs file — the following piece of code illustrates how you can achieve this.

using System;

namespace IDG

{

    public class Global : System.Web.HttpApplication

    {

        protected void Application_Start()

        {

            var appHost = new IDGAppHost();

            appHost.Init();

        }

        protected void Application_End()

        {

        }

        protected void Session_Start(Object sender, EventArgs e)

        {

        }

    }

}

And you are done! You have created your first service using ServiceStack. You can now execute your service by pressing the F5 key and see it get started and display the supported operations in your web browser.

Now that the service is up and running, you may create a service client to consume your service. To consume your service, you would need to create a project that includes the ServiceStack.Client namespace. You can include this namespace using NuGet again.

Next, you can create an instance of the type of client you would like to use — this depends on the type of data your need.

var xmlClient = new XmlServiceClient(“http://localhost:4416/”);

var jsonClient = new JsonServiceClient(“http://localhost:4416/”);

ServiceStack has been well received by the community – you can take a look at the community resources wiki page to get to know more on ServiceStack.

In my future articles here, I’ll explore more on ServiceStack, the best practices that can be followed and also illustrate how it can be used in enterprise applications.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author