49

PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 2: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 3: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 4: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

C# 5.0 includes the async and await keywords to ease writing of asynchronous code

In Windows Store Apps, new Task-based methods are used for networking exclusively ,not supported on Windows Phone 8

Solutions for Windows Phone are available:

Page 5: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

API WP7.1 WP8 W8

System.Net.WebClient

System.Net.HttpWebRequest (async only)

System.Net.Http.HttpClient (NuGet) (NuGet)

Windows.Web.Syndication.SyndicationClient

Windows.Web.AtomPub.AtomPubClient

ASMX Web Services

WCF Services

OData Services

Page 6: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

using System.Net;...

WebClient client;

public MainPage(){

...client = new WebClient();client.DownloadStringCompleted += client_DownloadStringCompleted;

}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e){

this.downloadedText = e.Result;}

private void loadButton_Click(object sender, RoutedEventArgs e){

client.DownloadStringAsync(new Uri("http://MyServer/ServicesApplication/rssdump.xml"));}

Page 7: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

using System.Net;using System.Threading.Tasks;...

private async void LoadWithWebClient(){

var client = new WebClient();string response = await client.DownloadStringTaskAsync(

new Uri("http://MyServer/ServicesApplication/rssdump.xml"));this.downloadedText = response;

}

Page 8: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

using System.Net;using System.Threading.Tasks;...

// Create the HttpClientHttpClient httpClient = new HttpClient();httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

// Make the callHttpResponseMessage response = await httpClient.GetAsync(

"http://services.odata.org/Northwind/Northwind.svc/Suppliers");

response.EnsureSuccessStatusCode(); // Throws exception if bad HTTP status code

string responseBodyAsText = await response.Content.ReadAsStringAsync();

Page 9: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 10: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

Mobile devices are often connected to poor quality network connections

Best chance of success in network data transfers achieved by:

Avoid transferring redundant data

Design your protocol to only transfer precisely the data you need and no more

Page 11: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 12: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

Wire Serialization Format Size in Bytes

ODATA XML 73786

ODATA JSON 34030

JSON ‘Lite’ 15540

JSON ‘Lite’ GZip 8680

Page 13: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 14: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

http://sharpziplib.com/

http://sharpcompress.codeplex.com/

Page 15: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

var request = HttpWebRequest.Create("http://yourPC/Service.svc/Suppliers") as HttpWebRequest;

request.Accept = "application/json";

request.Method = HttpMethod.Get;

request.Headers["Accept-Encoding"] = "gzip";

HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

// Read the response into a Stream object.

System.IO.Stream stream;

stream = new GZipInputStream(response.GetResponseStream());

else

stream = response.GetResponseStream();

string data;

using (var reader = new System.IO.StreamReader(stream))

{

data = reader.ReadToEnd();

}

stream.Close();

Page 16: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 17: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 18: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 19: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 20: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e){

var request = HttpWebRequest.Create("http://myServer:15500/NorthwindDataService.svc/Suppliers") as HttpWebRequest;

request.Credentials = new Credentials("username", "password"); // override allows domain to be specified

request.Accept = "application/json;odata=verbose";

request.BeginGetResponse(new AsyncCallback(GotResponse), request);}

Provide your own UI to request the credentials from the user

If you store the credentials, encrypt them using the ProtectedData class

Page 21: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

private void StoreCredentials(){

// Convert the username and password to a byte[].byte[] secretByte = Encoding.UTF8.GetBytes(TBusername.Text + "||" + TBpassword.Text);

// Encrypt the username by using the Protect() method.byte[] protectedSecretByte = ProtectedData.Protect(secretByte, null);

// Create a file in the application's isolated storage.IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();IsolatedStorageFileStream writestream =

new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, file);

// Write data to the file.Stream writer = new StreamWriter(writestream).BaseStream;writer.Write(protectedSecretByte, 0, protectedSecretByte.Length);writer.Close();writestream.Close();

}

Page 22: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

// Retrieve the protected data from isolated storage.IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();IsolatedStorageFileStream readstream =

new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open, FileAccess.Read, file);

// Read the data from the file.Stream reader = new StreamReader(readstream).BaseStream;byte[] encryptedDataArray = new byte[reader.Length];

reader.Read(encryptedDataArray, 0, encryptedDataArray.Length);reader.Close();readstream.Close();

// Decrypt the data by using the Unprotect method.byte[] clearTextBytes = ProtectedData.Unprotect(encryptedDataArray, null);

// Convert the byte array to string.string data = Encoding.UTF8.GetString(clearTextBytes, 0, clearTextBytes.Length);

Page 23: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 24: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

http://localhost

http://localhost

Page 25: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 26: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

C:\Users\yourUsername\Documents\IISExpress\config\applicationhost.config

<sites>

<binding protocol="http" bindingInformation="*:18009:localhost" />

<binding protocol="http" bindingInformation="*:18009:YourPCName" />

YourPCName:18009

Page 27: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

netsh advfirewall firewall add rule name="IIS Express (non-SSL)" action=allow protocol=TCP dir=in localport=8080

yourPC

8080

http://msdn.microsoft.com/en-us/library/ms178109(v=VS.100).aspx

Page 28: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 29: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 30: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

Supported on Lumia 520, 620, 625, and 720 with the 'Amber' firmware update

Support coming to 82x, 92x and 1020 with the ‘Black’ update (roll out soon!)

Lumia 1520 comes with the ‘Black’ update

Page 31: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

App to device

App to app

Page 32: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 33: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

// Register for incoming connection requestsPeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested;

// Start advertising ourselves so that our peers can find usPeerFinder.DisplayName = "TicTacToe BT";PeerFinder.Start();

Page 34: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

// Register for incoming connection requestsPeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested;

// Start advertising ourselves so that our peers can find usPeerFinder.DisplayName = "TicTacToe BT";PeerFinder.Start();

Page 35: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

StreamSocket socket;

async void PeerFinder_ConnectionRequested(object sender, ConnectionRequestedEventArgsargs){

if ( args.PeerInformation.DisplayName == "TicTacToe BT" ){

socket = await PeerFinder.ConnectAsync(args.PeerInformation);PeerFinder.Stop();

}}

Page 36: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 37: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 38: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 39: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

Connect devices

Acquire content

Exchange digital objects

Page 40: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 41: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

ProximityDevice device = ProximityDevice.GetDefault();

// Make sure NFC is supportedif (device != null){

PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnectionStateChanged;

// Start finding peer apps, while making this app discoverable by peersPeerFinder.Start();

}

Page 42: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

ProximityDevice device = ProximityDevice.GetDefault();

// Make sure NFC is supportedif (device != null){

PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnStateChanged;

// Include the Windows 8 version of our app as possible peerPeerFinder.AlternateIdentities.Add("Windows", "my Win8 appID");// Start finding peer apps, while making this app discoverable by peersPeerFinder.Start();

}

Page 43: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

void OnTriggeredConnStateChanged(object sender, TriggeredConnectionStateChangedEventArgs args) {

switch (args.State) {case TriggeredConnectState.Listening: // Connecting as host

break;case TriggeredConnectState.PeerFound: // Proximity gesture is complete – setting up link

break;case TriggeredConnectState.Connecting: // Connecting as a client

break;case TriggeredConnectState.Completed: // Connection completed, get the socket

streamSocket = args.Socket;break;

case TriggeredConnectState.Canceled: // ongoing connection cancelled break;

case TriggeredConnectState.Failed: // Connection was unsuccessfulbreak;

}}

Page 44: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

PeerFinder.AllowBluetooth = true;PeerFinder.AllowInfrastructure = true;

Page 45: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 46: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

Windows.Networking.Proximity.ProximityDevice proximityDevice;

long publishedMessageId = -1;

private void PublishUriButton_Click(object sender, RoutedEventArgs e)

{

if (proximityDevice == null) proximityDevice = ProximityDevice.GetDefault();

// Make sure NFC is supported

if (proximityDevice != null) {

// Stop publishing the current message.

if (publishedMessageId != -1) {

proximityDevice.StopPublishingMessage(publishedMessageId);

}

// Publish the new one

publishedMessageId = proximityDevice.PublishUriMessage(new Uri("zune:navigate?appid=351decc7-ea2f-e011-854c-00237de2db9e"));

}

}

Page 47: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,
Page 48: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,

+ runner up prizes

Windows Phone Quiz

WIN £50 In the community area (next to Twilio)

Straight after this session (at 10:05)

Hosted by Windows Phone User Group

http://wpug.net/ @wpug

(store gift card)

Page 49: PowerPoint Presentationandywigleyblog.azurewebsites.net/wp-content/... · C# 5.0 includes the async and await keywords to ease writing of asynchronous code In Windows Store Apps,