0% found this document useful (0 votes)
51 views52 pages

WCF vs ASP.NET Web API Overview

This document provides an overview of a presentation given by Adnan Masood on WCF and ASP.NET Web API. The presentation compares WCF and Web API, discusses migrating from WCF to Web API, and covers architectural questions to consider when choosing between the two frameworks. It also includes slides on the history of web services, SOAP, REST, and SignalR.

Uploaded by

Sonu Sathyadas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views52 pages

WCF vs ASP.NET Web API Overview

This document provides an overview of a presentation given by Adnan Masood on WCF and ASP.NET Web API. The presentation compares WCF and Web API, discusses migrating from WCF to Web API, and covers architectural questions to consider when choosing between the two frameworks. It also includes slides on the history of web services, SOAP, REST, and SignalR.

Uploaded by

Sonu Sathyadas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction
  • Agenda
  • Architectural Questions
  • History of Web Services
  • History of SOAP
  • Tools Evolution
  • Comparison of SOAP and REST
  • SignalR
  • Migrating from WCF to ASP.NET Web API
  • Integrated Stack
  • Protocol Comparison
  • SOAP and REST
  • XML vs JSON
  • Public APIs
  • WebAPI
  • HTTP Method Meanings
  • Configure Transport
  • Return Codes
  • HTTP Status Codes
  • Security
  • Testing WebAPI
  • Consuming WebAPI
  • Developing WebAPI and iOS Together
  • Consuming WebAPI in iOS
  • XML Parsing in iOS
  • Call WebAPI from Android
  • JSON in Android
  • XML Parsing in Android
  • WebAPI as Persistence
  • Comparison

WCF & ASP.

NET Web API An


Architects Primer
Presented at Southern California .NET Architecture User
Group; Feb 21st 2013.

Adnan Masood MS (CS). MCSD. MCPD.


Software Architect & Doctoral Candidate
[Link]
[Link]@[Link]

About Me
Adnan Masood works as a system architect / technical lead for Green dot
Corporation where he develops SOA based middle-tier architectures,
distributed systems, and web-applications using Microsoft technologies. He
is a Microsoft Certified Trainer holding several technical certifications,
including MCPD (Enterprise Developer), MCSD .NET, and SCJP-II. Adnan
is attributed and published in print media and on the Web; he also teaches
Windows Communication Foundation (WCF) courses at the University
of California at San Diego and regularly presents at local code camps and
user groups. He is actively involved in the .NET community as cofounder
and president of the of San Gabriel Valley .NET Developers group.

Adnan holds a Masters degree in Computer Science; he is currently a


doctoral student working towards PhD in Machine Learning; specifically
discovering interestingness measures in outliers using Bayesian Belief
Networks. He also holds systems architecture certification from MIT and
SOA Smarts certification from Carnegie Melon University.

Abstract
WCF vs. [Link] Web API An Architects Primer
[Link] Web API is a framework that makes it easy to build HTTP services that reach a
broad range of clients, including browsers and mobile devices. The new [Link] Web
API is a continuation of the previous WCF Web API projection. WCF was originally
created to enable SOAP-based services and other related bindings. However, for simpler
RESTful or RPCish services (think clients like jQuery) [Link] Web API is a good
choice.
In this meeting we discuss what do you need to understand as an architect to implement
your service oriented architecture using WCF or [Link] web API. With code samples,
we will elaborate on WCF Web APIs transition to [Link] Web API and respective
constructs such as Service vs. Web API controller, Operation vs. Action, URI templates vs
[Link] Routing, Message handlers, Formatters and Operation handlers vs Filters,
model binders. WebApi offers support for modern HTTP programming model with full
support for [Link] Routing, content negotiation and custom formatters, model binding
and validation, filters, query composition, is easy to unit test and offers improved Inversion
of Control (IoC) via DependencyResolver.

Agenda
Difference between Web API and WCF REST Services
How to Migrate from WCF Web API to [Link] Web API
Model for RESTFul Maturity
WCF or Web API confusing?
WCF evolution and strengths
What [Link] Web API brings to the table?
Architectures and comparing non functional requirements

Architectural Questions
What is the purpose of the WebAPIs?
Why do we need REST HTTP services? Whats wrong with
SOAP-over-HTTP?
Why did the WebAPIs move from WCF to [Link] MVC?
Is there still a use for WCF? When should I choose Web APIs
over WCF?

History of Web Services


1989 - Tim Berners-Lee invents HTTP/HTML
1998 - XML 1.0, SOAP begins ratification
2001 - SOAP standard
2000 - Fielding dissertation on REST

History of SOAP
Before SOAP we did this
HTTP GET/POST with Plain Old XML (POX)
Out-of-band exchange of DTD or schema

SOAP evolved to provide us


Specifications
Tooling
Metadata
Productivity
location transparency

Tools Evolution
[Link] Web Services (ASMX)
Web Services Enhancements (WSE)
.NET 3.0 => WCF = SOAP+WS*
.NET 3.5 => WCF = SOAP+WS*/ HTTP
WebHttpBinding, contract attributes, JSON
REST Starter Kit (Codeplex)
WCF 4 => Features from starter kit
WCF Web API => [Link] Web API

SignalR
What is [Link] SignalR
[Link] SignalR is a new library for [Link] developers that makes it
incredibly simple to add real-time web functionality to your applications.
What is "real-time web" functionality? It's the ability to have your serverside code push content to the connected clients as it happens, in realtime.
You may have heard of WebSockets, a new HTML5 API that enables bidirectional communication between the browser and server. SignalR will
use WebSockets under the covers when it's available, and gracefully
fallback to other techniques and technologies when it isn't, while your
application code stays the same.
SignalR also provides a very simple, high-level API for doing server to
client RPC (call JavaScript functions in your clients' browsers from
server-side .NET code) in your [Link] application, as well as adding
useful hooks for connection management, e.g. connect/disconnect
events, grouping connections, authorization.

The WCF Web API => [Link]


Web API
WCF Web API -> [Link] Web API
Service -> Web API controller
Operation -> Action
Service contract -> Not applicable
Endpoint -> Not applicable
URI templates -> [Link] Routing
Message handlers -> Same
Formatters -> Same
Operation handlers -> Filters, model binders

Integrated stack
Modern HTTP programming model
Full support for [Link] Routing
Content negotiation and custom formatters
Model binding and validation
Filters
Query composition
Easy to unit test
Improved Inversion of Control (IoC) via DependencyResolver
Code-based configuration
Self-host

SOAP
Simple Object Access Protocol
Uses a standard XML Schema over HTTP
Extremely cross platform compatible
Extremely Slow

REST
Representable State Transfer
Uses standard HTTP
Can use any text format including XML

XML vs JSON
XML tag based document formatting
Javascript Notation by Douglas Crockford
JSON less verbose than XML, more lightweight
Mobile devices have limited bandwidth

Public APIs
Twitter
Facebook
Flickr
Amazon
iTunes

WebAPI
Available now as Nuget Package
Built-in as part of MVC 4
Take advantage of HTTP features directly

Default route will use http method for action


Controller/action/id
API/Controller/id GET/POST/PUT/DELETE

HTTP methods as Actions

HTTP Method meanings


Get - Return an existing document
Post - Create a new document
Put - Update a document
Delete - Self explanatory

Configure Transport
Set Xml or JSON based on Content-Type or Accept
header
Accept: application/xml
Can also use Odata

Return Codes
Now have the ability to specify return codes beside 200
HttpResponseMessage<YourEntity>
[Link] 201
[Link] = new Uri()

Http Status codes


201 Created
200 Success/204 Success but No Content
403 Not authorized
404 Does not exist
500 Server Error
301 Uri Moved

Security
[Authorize()]
https over port 443
Security Tokens
OAuth

Testing WebAPI
Download Fiddler2
Firebug (Firefox)
Chrome
On Mac use CocoaRestClient

Consuming WebAPI
Web Apps ([Link], MVC, PHP, Java, ColdFusion,
Ruby(Rails), Python, Perl(if you are masochistic))
JavaScript/JQuery
Mobile (iOS, Android, WP7, Blackberry OS)

Develop WebAPI and iOS on


Same Computer
Parallels or VMWare
Set Network Adapter to Bridged
Run Visual Studio as Administrator
Host on IIS (do not use IIS Express or Casini)

Consuming WebAPI
in iOS
Use NSURLConnection delegate or GCD
Show progress while waiting on response
Use JSON over XML
NSJSONSerialization class (new in iOS 5)

XML Parsing in iOS


NSXMLParser (Slowest)
libxml2 (C api)
TBXML (DOM, Fastest, no write or xpath)
TouchXML (DOM supports xpath, no write)
KissXML (based on Touch, can write)
GDataXML (DOM, from Google)
RaptureXML (DOM, supports xpath)

Create DefaultHttpClient();
Create request with HttpGet(Url);
Create response handler BasicResponseHandler();
[Link](request, handler);

Call WebAPI from Android

JSON in Android
use the JSONObject to parse
JSONObject jo = new JSONObject(jString);
[Link](car);
[Link](cars);

XML Parsing in Android


DOM, SAX and Pull
W3C Dom parser
Standard Java Sax Parser
SJXP (Pull parser)

WebAPI as persistence
Dont use WebAPI as default persistence on Mobile
Both Android and iOS have device persistence
local storage, CoreData and SQLite
iCloud to sync between iOS devices

Demo

Slides courtesy Michelle L. Bustamante

Comparison
Transport Coupling
HTTP is an application protocol, not just a transport protocol
TCP, named pipes, MSMQ, UDP are transport only
WCF is decoupled, message can traverse any

Performance
Sometimes a faster protocol/serialization mechanism is needed

Slides courtesy Michelle L. Bustamante

Security
Web API

HTTP Services
HTTPS / SSL
Authorization header or custom headers
OAuth 2.0

WCF Services

HTTPS / SSL
SOAP Message Security
WS-Trust
OAuth 2.0
Slides courtesy Michelle L. Bustamante

Error Handling
Mostly automatic
Helpful to control how things are returned to Ajax clients
Setting status code and message

Slides courtesy Michelle L. Bustamante

Hosting
IIS or Self hosting

Slides courtesy Michelle L. Bustamante

Feature Comparison
Productivity

Design effort
Complexity
Client code and proxy generation
Communication stack

State
Both should be stateless
Caching
Built in to HTTP, but beware

The WS* Overload

Slides courtesy Michelle L. Bustamante

SOAP vs REST Focus on the


necessities

Slide courtesy Michelle L. Bustamante

Web API Selling Points


If we need a Web Service and dont need SOAP, then [Link]
Web API is very useful.
Web API - Used to build simple, non-SOAP-based HTTP Services
on top of existing WCF message pipeline.
Web API - No need for configurable like WCF REST services
Web API - No need for Data contracts
Web API - Could create fully blown REST Services
Simple service creation with Web API. With WCF REST Services,
service creation is difficult.
WCF is any wire protocol. Web API is focused at one thing, being
easy to define and expose and consume, while also facilitating
doing things in a RESTful way.
Web API is light weight architecture.

The hypermedia Venn Diagram

The unified model? Kinda, sorta


Obsolete

Revision - WCF to [Link] Web


API

ApiController (!=Controller, no common BaseClass)


[Link] Routing (MapHttpRoute)
Convention over Configuration
Web API to go / NuGet Packages
Web API hosted in [Link]: AspNetWebApi
Self-hosted Web API: [Link]
HttpClient including XML and JSON formatters:
[Link]
JsonValue for navigating and manipulating JSON:

[Link]
Go Live License

How to Migrate from WCF Web


API to [Link] Web API
[Link]
%20from%20WCF%20Web%20API%20to%[Link]%20
Web%20API

Competing with [Link]?

Resources & Credits

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Thanks to Michele Leroux Bustamantes slide-deck from Windows Azure connections,
March 26-29, 2012 Las Vegas, NV which I thoroughly enjoyed.
Ida Flatows article on web API [Link]
Alexander Zeitlers Web API Round up
David Fekke Web API - [Link]/Media/Default/powerpoint/[Link]

Summary
Choose wisely and quantitatively; avoiding the shiny object
syndrome.
Enterprise WCF implementations will continue to be
important
The trend to HTTP services is here to stay, embrace it
If you are starting from scratch for a mobile / web heavy
service, look at HTTP services first

Thank You!
Adnan Masood
[Link]@[Link]
@adnanmasood
Blog: [Link]
Pasadena .NET User Group: [Link]

WCF & ASP.NET Web API – An 
Architect’s Primer 
Presented at Southern California .NET Architecture User 
Group; Feb 21st 201
About Me 
Adnan Masood works as a system architect / technical lead for Green dot 
Corporation where he develops SOA based mi
Abstract 
WCF vs. ASP.NET Web API –  (http://www.scdna.org/)(http://www.scdna.org/) An Architect’s Primer (http://www.scdna.o
Agenda 
Difference between Web API and WCF REST Services 
How to Migrate from WCF Web API to ASP.NET Web API 
Model for RE
Architectural Questions 
What is the purpose of the WebAPIs? 
Why do we need REST HTTP services? What’s wrong with 
SOAP-ov
History of Web Services 
1989 - Tim Berners-Lee invents HTTP/HTML 
1998 - XML 1.0, SOAP begins ratification 
2001 - SOAP stan
History of SOAP 
Before SOAP we did this…  
HTTP GET/POST with Plain Old XML (POX)  
Out-of-band exchange of DTD or schema
Tools Evolution 
 
ASP.NET Web Services (ASMX)  
Web Services Enhancements (WSE)  
•.NET 3.0 => WCF = SOAP+WS*  
•.NET 3.

You might also like