public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; }...

29

Transcript of public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; }...

Page 1: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.
Page 2: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

ASP.NET Web APIWeb Services for Web Sites and Modern & Mobile AppsBrady GasterWindows Azure Technical Evangelist

DEV-B360

Page 3: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

IntroductionsIt always helps to know who you’re dealing with, so here’s a little background on me.

@bradygaster

1 Wife (Gina)

2 Chihuahuas (Lola & Nico)

Windows Azure Technical Evangelist

bradygaster.com

2 Sons (Gabe & Lucas)

Originally from NC (hence the funny accent)

2 Guitars (Fender, Gretsch)

Page 4: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

AgendaWhat are we going to talk about today?

Web API Test Clients

Modern Apps with Windows 8

Mobile Apps with Xamarin

Example Scenario

Portable Class Libraries

Mobile Apps with Windows Phone 8

Authentication with Web APICross Origin Resource Sharing

Page 5: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Example ScenarioHere’s a look at the database we’re going to expose using a Web API

Page 6: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Example ScenarioEntity Framework Class View Model Classpublic class UsLocationRecord{ public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string State { get; set; } public DbGeography Location { get; set; }}

public class UsLocation{ public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string State { get; set; } public double Latitude { get; set; } public double Longitude { get; set; }}

This doesn’t serialize too well, so we need a View Model

There. That’s better.

Page 7: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Running the Sample API

Page 8: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Web API Test ClientNuGet Package

NuGet Package Link: http://aka.ms/Kuqhny

Any browser

Templates are .CSHTML files

Introduction and how-to:http://aka.ms/webapitestclientintro

Page 9: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

PostmanChrome Plugin

http://www.getpostman.com/

Page 10: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Setting up and using the Web API Test Client

Page 11: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

.NET is supported by many platformsMany tricks have been employed to write reusable components that work on all of them

Code Files for “reusable” classes

Models.SilverlightModels.Phone Models.WindowsModels

UX.SilverlightUX.Phone UX.WindowsUX.Web

ApiClient.SilverlightApiClient.Phone ApiClient.WindowsApiClient.Web

“reused code” isn’t the same as“reusable code”

Page 12: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Portable Class LibrariesCommon code that can be shared across all the .NET environments

Code Files for reusable classes

UX.SilverlightUX.Phone UX.WindowsUX.Web

Common PCL

Page 13: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Writing a Portable Class Library to Provide a client API

Page 14: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Client Platform

Modern and Mobile ClientsWindows 8 Store Apps and Windows Phone 8 Apps can use the PCL

Windows 8Store App

Windows Phone 8 App

PCL HTTP

Server Platform

Web API Controller

Page 15: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Using Web API in Modern Apps with Windows 8

Page 16: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Using Web API in Mobile Apps with Windows Phone 8

Page 17: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

XamarinC# everywhere, even on iOS and Android

Page 18: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Communicating with a Web API using a C# Android Client

Page 19: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Sharing Data Across APIs Should be EasyMy web site has an API, your web site has an API, let’s make a mash-up!

foo.com bar.comHTML

AJAX

HTTP Request

AJAX(from pagehosted onfoo.com)

?JSON

Page 20: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

CORS

Cross Origin Resource SharingOnce CORS is enabled, cross-domain communication via AJAX/HTTP/JavaScript is possible

foo.com bar.comHTML

AJAX

JSON

AJAX

JSON

Page 21: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Enabling Cross Origin Resource Sharing

Page 22: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Web API & AuthenticationThe framework & tooling release will make API simple & allow for federationWeb API app configured to use OAuth Bearer TokensClient app asks Web API what OAuth providers it supportsUser selects provider and is redirected to loginUpon login, user redirected back to API site with credential stringCredential string is parsed into a claim, then an identityUserInfo is passed back to client where it is persistedUserInfo’s access token is attached to each API request as a Bearer Token HeaderAPI performs authorization using the Bearer Token Header

Page 23: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Demo

Walking through a Multi-client Authentication Process

Page 24: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Related contentBreakout Sessions (session codes and titles)Hands-on Labs (session codes and titles)Product Demo Stations (demo station title and location)Related Certification ExamFind Me Later At...

Page 25: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Track resourcesGeneric Client REST Wrapperhttps://gist.github.com/bradygaster/5682105Web API Test Client How-tohttp://aka.ms/webapitestclientintroASP.NET on CodePlexhttp://aspnetwebstack.codeplex.com/Channel Web Camps TV EpisodesKatana - http://aka.ms/C7xunhCORS - http://aka.ms/Q3uthfOData - http://aka.ms/Txrdhu, http://aka.ms/V4n6cvYao’s Web API Tour - http://aka.ms/G24zpi

Page 26: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

msdnResources for Developers

http://microsoft.com/msdn

LearningMicrosoft Certification & Training Resources

www.microsoft.com/learning

TechNet

Resources

Sessions on Demandhttp://channel9.msdn.com/Events/TechEd

Resources for IT Professionalshttp://microsoft.com/technet

Page 27: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

Complete an evaluation on CommNet and enter to win!

Page 28: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

MS tag

Scan the Tagto evaluate this session now on myTechEd Mobile

Page 29: public class UsLocationRecord { public int Id { get; set; } public string ZipCode { get; set; } public string City { get; set; } public string.

© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.