RESTful Web Service Building Diary (I) --- Survey of the Web Service

28 Jun 2016

Introduction

As an intern in MeridianLink, I was asked to build a RESTful web service for voice message authorization as part of the LoansPQ in C# and .Net framework. This series will keep track of the things I learn and my development process.

Web Service

A web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Java and Python, or Windows and Linux applications) is due to the use of open standards.

SOAP vs RESTful

Simple Object Access Protocol (SOAP) and Representational State Transfer (REST) are two answers to the same question: how to access Web Services. SOAP is originally developed by Microsoft while REST is the newcomer. SOAP and REST share similiarities such as they both use HTTP, and they also have some differences.

SOAP is developed by Microsoft and relies exclusively on XML to provide messaging services. SOAP is highly extensible ut the XML used to make requests and receive responses can become extremely complex. It has the Web Services Description Language (WSDL) to specify how the Web service works, has built-in error handling and supports using of SMTP for transports.

REST provides a lighter weight alternative. Instead of using XML, it relies on simple URL and HTTP methods. Also, it can not only output the data in XML, but also in CSV, JSON and RSS.

In conclusion, SOAP is a more heavy weight choice while REST is more flexible, they both have their advantages and disadvantages, make wise choice based on your application. For me, REST is a better choice.

RestSharp

RestSharp is a simple open source REST and HTTP API Client for .NET. Crafting an HTTP request can be done in several ways, with the most common way being the use of System.Net.Http.HttpClient or the simpler System.Net.WebClient. An alternative of it is the HttpClient, but it only support .NET Framework 4.5. RestSharp also has the automatic deserializeaton properity. It’s very easy to use, and supports cross platform.

ASP.NET Web API

ASP.NET Web API is a framework that makes it easy to build HTTP services such as RESTful applications on the .NET Framework.

It uses MVC framework. ASP.NET Web API can automatically serialize your model to JSON, XML or some other format. Its controller inherit the ApiController class instead of the traditional Controller class, it handles HTTP request, the public methods of the controller are called action methods or simply actions.

Also, it sets a default route for you, so if you follow the default route, you don’t need to specify the route. For connect to the database, it use the Entity Framework, which is very easy to use and support Data Transfer Objects. Apart from these, it supports OData, Model serialization, error handling, testing and debugging, authorization and more advanced topics. It has very detailed documentations, videos and sample projects. An alternative is a third-party framework called ServiceStack.

Testing

There are several ways to test the api, below lists some options.

Another easy way is just using jquery, getJSON or ajax will work. Also, ASP.NET provides unit test tools.

Conclusion

After browsering various materials, I finally decide to use ASP.NET Web API to implement my RESTful api. RestSharp may be useful when I call other services. Next step will be investage different voice message services, understand the service requirements and design the system architecture.

References