24
Razvoj REST API-ja za Windows Azure koristeći ASP.NET Web API Radenko Zec, Lanaco d.o.o October 24rd, 2012

Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Embed Size (px)

Citation preview

Page 1: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Razvoj REST API-ja za Windows Azure

koristeći ASP.NET Web APIRadenko Zec, Lanaco d.o.o

October 24rd, 2012

Page 2: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Cilj prezentacije

• napraviti “Real World” Web API koristeći

ASP.NET Web API

Page 3: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Šta je REST ?

• to je stil softverske arhitekture, za distribuirane

hypermedia sisteme kao što je „World Wide Web“

• nije protokol ni standard

Page 4: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Šta je ASP.NET Web API ?

• Frejmvork koji omogućava lak razvoj HTTP

servisa kojima se može pristupati sa velikog

broja različitih klijenata uključujući browsere i

mobilne uređaje

Page 5: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Da li je ASP.NET Web API =

RESTful service?

• ASP.NET Web API ne diktira stil arhitekture

• ali vi možete da razvijete RESTful service

pomoću ASP.NET Web API- ja

Page 6: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Richardson-ov model zrelosti

REST-a

Page 7: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Zašto Web API ?

+

Page 8: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Skaliranje u oblaku

Page 9: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Koje metode ASP.NET Web API

podržava

Metod Akcija HTTPMetod Relativni URI

Get Vraća listu svih

kontakata

GET /api/contacts

Get Vraća kontakt na

osnovu id-a

GET /api/contacts/id

Add Dodaje novi

kontakt

POST /api/contacts

Update Mijenja selektovani

kontakt

PUT /api/contacts

Delete Briše kontakt DELETE /api/contacts/id

Page 10: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Rast Web API-ja

Page 11: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Kako implementirati Web API

• Nasljediti klasu ApiController

• Implementirati akcije

– akcije se mapiraju na HTTP metode

– stavite prefix ispred metoda za željeni HTTP method

(PostCustomer)

Page 12: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

DEMO

Page 13: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Action parameters

• prosti tipovi se dobijaju iz URI-ja

– route data, query parameters

http://api.mssinergija.net/kontakt?kod=nesto

• kompleksni tipovi – request body

– MediaTypeFormatters koriste se za deserijalizaciju

Request Body-ja zavisno od content type-a

– JSON i XML su podržani pod default-u

Page 14: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Validacija

• Validacija se vrši na podacima prilikom svakog

request-a

• greške prilikom validacije se snimaju u

ModelState

• provjerite ModelState.IsValid

Page 15: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Filteri

• Tipovi filtera :

– Action filters (pokreću se prije i nakon poziva akcije)

– Authorization filters (pokreću se prije model binding-a i

služe za autorizaciju korisnika)

– Exception filters (generišu odgovor u slučaju greške na

centralizovan način)

• Oni mogu biti konfigurisani globalno, po

kontroleru i po akciji

Page 16: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

HTTP Dispatcher

Invoke Action

Action filters

Model Bind

Select action Exception filters

Route to controller Formatting

Request Response

Authorization filters

Page 17: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Opis Web API-ja

• koristi se IApiExplorer servis da bi se dobio

runtime opis Web API-ja

• korisno prilikom izrade help stranica, test

klijenata, alata i sl.

• http://nuget.org/packages/Microsoft.AspNet.Web

Api.HelpPage

Page 18: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

IApiExplorer

public ActionResult Index()

{

var apiExplorer =GlobalConfiguration.Configuration.Services.GetApiExplorer();

return View(apiExplorer);

}

Page 19: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Hosting

• dva načina hostovanja

– ASP.NET Web Application (IIS)

– Self-host (npr. console application, Windows service,

Azure Worker Role, itd.)

• hostovanje u memoriji je korisno za integration

testiranje servisa

Page 20: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

DEMO

Page 21: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

http://blog.developers.ba

@radenkozec

Page 22: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Hvala na pažnji.

Page 23: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Help us choose the best Sinergija lecturer! HTC and Microsoft will award you

– at the conference end, we’ll give one HTC Windows Phone 8X to someone

from the audience – randomly.

Go to www.mssinergija.net, log in and cast your votes!

You can rate only lectures that you were present at, just once. More lectures you rate, more chances you have.

Please use computers at the front of this room, or rate lecture from your phone or home computer, at Sinergija portal.

This prize contest will end at Thursday, October 24th at 21:00. Winner will be announced at the official Sinergija web portal,

www.mssinergija.net

Please rate this lecture

and WIN Windows Phone 8X by

HTC!

Powered by:

Page 24: Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API

Microsoft and

Open Sourcegateway for deeper

exploration of

open source

engagementshttp://www.microsoft.com

/openness

Port25blogs from the

platform

community and

the OSS Lab

teams http://Port25.technet.com

Codeplexresources for

developers and

consumers of

open source

projectshttp://www.codeplex.com

Interoperability

Bridgestechnical collaborative

works http://www.interoperabilit

ybridges.com

Open Upcross-Industry

Interoperability

and Standards

activitieshttp://www.microsoft.com

/interop/openup

Shared Sourceportal for

programmaticall

y sharing codehttp://www.microsoft.com

/sharedsource

ODataopen source starter kit

for Internet

publishing of

Government

datasets using

the Open Datahttp://ogdisdk.cloudapp.ne

t

Open Specprotocols, file formats,

standards,

technical

specificationshttp://www.microsoft.com

/openspecification

s

BizSparkProgram for Start-Up

companies from

both

commercial and

open source

backgroundshttp://www.microsoft.com

/bizspark

Openness and Interoperability @Microsoft

How can I receive up-to-date Openness announcements from Microsoft?

In addition to the websites above, you can receive regular updates to

Microsoft’s openness, interoperability and standards efforts via the following

channels:

• http://blogs.technet.com/b/openness/

• http://blogs.msdn.com/b/interoperability/

• http://twitter.com/OpenAtMicrosoft

• http://port25.technet.com

• http://channel9.msdn.com/Blogs/Interoperability