May 20, 2011
RESTful Web Services using C# and WCF
By DineEngine

When developing a web services portion of a mobile targeted API, there are several key features that can make a developer’s task much easier. The WCF platform provides a developer with many such features, as well allowing the framework to be written in a familiar – and in my opinion, excellent – language. In addition to making the API itself simpler to develop, the nature of RESTful web services also make developing the phone side of an API simpler. Instead of developing an API specific to a device, or to take advantage of only a single output or input format, WCF can be use to build a standardized RESTful web service that is capable of producing and consuming data in XML or JSON, and can be extend to produce or consume arbitrary data types as necessary.

One feature that WCF provides is automatic serialization, and deserialization, of POCOs (plain old C# objects) into XML or JSON representations. This feature allows the developer to simply create a family of POCOs that define a particular API’s data model, and let the framework convert to and from these objects. The ease of this conversion is exemplified by allowing the developer to exclusively develop using POCOs; REST endpoints simply return POCOs and accept POCOs as parameters. This means that a developer can query a variety of sources for data (database, another API, etc.), populate the POCOs particular to his web service, and return the POCO from the method he just wrote. All serialization is handled and defined by the WCF service annotations.

Another advantage that WCF provides developers is the ability to simply define “routes” or templates for the API endpoints. The template defines the overall structure of the URL request, and can be used to parse request parameters from the URL itself, which are then passed to the matching service method. For example, a service template like “libraries/{libraryId}/books.xml” and an API URL request like “http://www.foo.com/libraries/5/books.xml” will pass 5 to the service method associated with this template. Any POCO returned by the service method will be automatically serialized to XML. So the only code a developer must write is the code necessary to lookup the books for the particular library (where libraryId=5), populate a list of POCO Book objects with the results, and return the list of Books. WCF handles the parsing of the URL request, dispatching the parameters to the associated service method, and serializing the list of POCOs to an XML representation.

WCF also integrates well with other .NET features and, in general, the entire .NET framework. For example, a developer can choose to use a low level database connection, or he can choose to use the database designer tool in Visual Studio to generate database classes and connections for him. He can use LINQ to query a database or use SQL statements. He can use an external library through a DLL or compile a library into the service itself. Essentially, using the WCF framework is very similar to writing standard C# .NET applications, complete with all the features a developer expects.

Additionally, in the development phase, Visual Studio Server can be used to rapidly develop and test a growing web service. A developer simply writes some code, perhaps connecting a new endpoint and method to the API, builds the project, and Visual Studio will automatically start its own server and deploy the service to it. By default this is a local connection, but Visual Studio can also be configured to deploy anywhere including a local IIS server or a remote server. This allows the developer a one-push button to test a simple deployment of the service, to run automated tests, or to do a simple visual sanity check on the data returned by the API.

Overall, WCF, C#, .NET, and Microsoft SQL Server integrate very well together to define a single, solid foundation upon which to build a RESTful API. It is possible to build a WCF service with several complex databases or no database at all. It can be used to build a web service with a hundred API endpoints or just a few. It can even be used to develop an API that composes several “services” that each have a specific purpose. In any case, and no matter the complexity, WCF is a great choice for any RESTful API service for its ease of use, excellent platform integration, and standards compliant methodology.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Trusted by top brands.

Get Started with DineEngine.

Contact Us Now Find Out How Chepri Can Help Your Team. Ask Us More About RESTful Web Services using C# and WCF.

(800) 338-8102

733-C Lakeview Plaza Blvd. Worthington, OH 43085.