Skip to content

Latest commit

 

History

History
87 lines (66 loc) · 4.13 KB

42.md

File metadata and controls

87 lines (66 loc) · 4.13 KB

Introducing ServiceStack

It’s about time I write a post introducing the open-source project I’ve been working for a while now:

  • Service Stack
  • google code project

In a one-line summary it is a framework for rapidly developing re-usable SOA-style web services. It encompasses a number of best-practices and techniques I’ve learned over the years in creating web services, namely:

  • Low Coupling – Adding a new service only requires adding three classes: The request and response DTO’s and the Service Handler that handles the request.
  • Convention over Configuration – You really only need to write service-specific logic, there is no extra plumbing and boiler-plate code required and you do not need to modify any existing classes to make your service available.
  • C# classes first – There is an argument for writing your webservices XSD interface first and then using that to generate your DTO’s, In reality though C# POCO DataContract’s with their customizable attributes, auto properties and nullable types etc, make for a pretty good DSL to create your service interface. Doing this eliminates the need for any generated code and your WSDL and XSD are always uptodate as they are generated from the DTO’s themselves
  • Re-Usable Services – By only needing to write DataContract’s for your webservices definition, we’re able to expose your service on a number of different endpoints. As of this time ServiceStack supports calling your webservice over SOAP 1.1, SOAP 1.2, REST + POX, REST + JSON out of the box. This allows the same service you create for your windows application to also be used by your AJAX application without any additional effort
  • Encourages SOA-style services – Unfortunately by using a method signature to define your service, WCF encourages RPC-Style services. Although conceptually easier for a new developer, this is generally bad practice for web services as whenever you cross a process-boundary you want to reduce the chatter as much as possible for better performance and scalability. It also encourages application-specific web services, i.e. the services you create are only useful for the client application that it was built for
  • Effectively all the features boil down to having to write the minimum amount of code necessary to implement your webservice while at the same time having your service available to the most common endpoints in-use today.

Service Stack Web Service Example

As no introduction is complete without an example, I’ve taken a simple example from the Service Stack Demo website to show how easy it is to create a web service with ServiceStack:

Uses POCO DataContract’s to create the web service interface:

[DataContract]
public class GetFactorial
{
    [DataMember] public long ForNumber { get; set; }
} 
 
[DataContract]
public class GetFactorialResponse
{
    [DataMember] public long Result { get; set; }
}

The service handler:

public class GetFactorialHandler : IService<GetFactorial>
{
    public object Execute(GetFactorial request)
    {
        return new GetFactorialResponse {
            Result = GetFactorial(request.ForNumber)
        };
    } 
 
    static long GetFactorial(long n)
    {
        return n > 1 ? n * GetFactorial(n - 1) : 1;
    }
}

Well that should be the proof in the pudding! With only the above code, this service is now callable via:

  • REST + XML
  • REST + JSON
  • SOAP 1.1
  • SOAP 1.2

Example client AJAX code:

var serviceClient = new JsonServiceClient(
"http://www.servicestack.net/ServiceStack.Examples.Host.Web/");
 
var request = { ForNumber: $("#txtGetFactorial").val() };
serviceClient.getFromJsonService("GetFactorial", request,
    function(e) {
        $("#serviceGetFactorial .result").html(e.result.Result);
    });

Example client C# code:

var serviceClient = new XmlServiceClient(
"http://www.servicestack.net/ServiceStack.Examples.Host.Web/");
 
var request = new GetFactorial { ForNumber = 3 };
var response = serviceClient.Send&lt;GetFactorialResponse&gt;(request);
Console.WriteLine("Result: " + response.Result.ToString());

For more info, I invite you to checkout the google code project.