Wednesday, December 11, 2013

Developing Services for Modern Applications with ASP.Net Web API

First of all, let me give you Microsoft's definition of "Modern applications" as the applications runs in the following platforms (not limited to):

Browser (written in Java script, Ajax)

Device ( xbox, blue ray player, TV set-tops etc.)

Smart Phone runs various operating like IOS, Android OS, WP

Tablets runs various operating systems like IOS, android OS, Window 8

refrigerators and cars runs on various platforms including Linux.

If you take a closer look at the apps available in these platforms in the market, you will find most of them depends on some kind of back-end service to operate. If an app only acts on its own, its usefulness would be limited. So how to design and develop services for apps running in these device is an strategically important issue for many software architects and software makers. Look at the running environment of these modern apps, you will find the commonality among these devices are (1) they all have limited bandwidth in term network connectivity ( Wi-Fi, 3G or 4G wireless network) if any. (2) the common language they understand is HTTP, not wsdl , not SOAP. (3) most of these apps are at hands of consumers who has limited knowledge about computer or configurations. With all these considerations, Web Service or WCF service would not be considered as good candidates. TCP/IP socket programming is too much towards the low layer, may not be efficient in term of productivity and not many programmers are knowledgeable about it .

To response to such technical landscape, Microsoft came out with a new technology call "ASP.Net Web API" and the current version is 2.0. so it is commonly referred as "ASP.Net Web API 2". it is not web service pre-said. to be more accurately, it is HTTP Service.

I am sure you can find tons of resource if you search these key words on the web, the save my energy on more useful content, I am not going to offer you a list here. as I said search "ASP.Net Web API"  on the web, you will get lots useful information from white paper, to presentation to sample app source code. In a nutshell, it is an extension of ASP.Net MVC. it has all kind of terms like model, controller. but no views for the reason that it act as an API library does not need views. ( IBM also has its own Web API technology based on Web Sphere in Java world)

There is a say "talk is cheap, show me the code!" , I like that very much, so let me show you some code for you to see how simple it is to build your service with "ASP .Net Web API 2" in Visual Studio..

public class ModuleController : ApiController

{

protected ServiceEngine _service;

public AdminController()

{

_service = new ServiceEngine();

}

public IEnumerable<Module> GetAllModules()

{

return _service.GetModules(false);

}

public IHttpActionResult GetModule(int id)

{

var model = _service.GetModule(id);

if (model == null)

{

return NotFound();

}

else

{

return Ok(model);

}

}

}

you can see here with around 10 line of code, I made a service with 2 functionalities . Here the ServiceEngine is a class that provide the data from your data store. if your data stores in SQL/Server, you could write your own ADO code, or use Enterprise Library Database Application Block or using Entity Framework. To show how cool I am, I chose to use the latest version of Entity Framework, EF 6.0. With EF6.0, the code for ServiceEngine code is as follow:

public IList<VModuleList> GetModuleCatalog()

{

return (new DataEntities().ModuleLists()

}

public Module GetModule(int id)

{

DataEntities context = new DataEntities ();

var module = context.Modules.Where(m => m.Id == id).FirstOrDefault();

var words = context.Words.Where(w => w.ModuleId == module.Id).ToList();

return module;

}

so, all in all, in less than 20 line of code, I made a service with 2 operations.

let's test it out.

run your app and hit your app site with the following address:

http://localhost:8817/api/Module/ and http://localhost:8817/api/Module/1

you will get some content similar to the following respectively:

[
{
"WordCount":677,
"Title":"First Grade Vocabulary",
"Description":"Complete List of First Grade Vocabulary",
"Id":0

},
{
"WordCount":479,
"ProductId":2,
"Title":"Kindergarten Vocabulary",
"Description":"Complete List of Kindergarten Vocabulary",
"Id":0,
}
]

{
"Words":[
{
"Spelling":"across",
"SampleSentence":"across the road, there is a big building.",
"ParentId":0,
"Id":1,

}
],

"Title":" Kindergarten Vocabulary",
"Description":"Complete List of Kindergarten Vocabulary",
"Id":1,
}

what are they? most of you might know they are JSON strings that can be de-serialized into an object. if you want them to be in XAML format, it is as simple as add one line in your client code, (I will talk about Web API client in my next article). In the service side, no code change is needed. For these who are interested at JSON, or JSON library, there would be another topic by itself. just let me know.

I have my service deployed to my home server, and I made my home server accessible to the public, if you are interested to try by yourself, let me know, I will provide you with the URL of the service.

Isn't it cool. If you are as excited as I am. you might want to ask where is it , how can I get it? well. it has been there for some time,

1. ASP.Net Web API was first released with Visual Studio 2012, In Visual Studio 2012, there is a project template under <Web><ASP.Net MVC 4.0 Web Application> call <Web API>

2. ASP.Net Web API 2 was released as part of Visual Studio 2013. In Visual Studio 2013, there is a project template under <Web><ASP .Net Web Application> call <Web API>

3. API 2 is also available as a stand-alone NuGet package for Visual Studio 2012, the package name is "Microsoft ASP.Net Web API2 Core",

4. you also can find other useful packages like "simple test client for ASP.Net Web API" , "Web API2 Client", "Web.API2 OData" etc. from NuGat library.

5. Best of all, it is open source, if you do not like some parts of it, you can download the source code and make changes yourself. if like to share your changes with others, you can submit your changes at codeplex.com

Another great point is that it is very easy to adopt it. In my case, starting from the point I thought I need to have some kind of service for my demo window store app and had no idea on how to do it, to the point I got the service up and running. it look less than a day. I started it last Saturday morning and at Sunday breakfast table I was so excited talking to my wife about the wonderful service I had developed, leaving alone I still spend time with my family on grocery shopping and play piano with my daughter and other weekends family activities.


Programming should be an enjoyable, make it so!


No comments:

Post a Comment