22
7 Session 5.3 Using Network Services

3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Embed Size (px)

Citation preview

Page 1: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

7

Session 5.3

Using Network Services

Page 2: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone

Topics Creating a network service Proxy objects and services Service contracts and interfaces Creating a client Adding a service reference to a

project Connecting to a service

Page 3: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone3

Creating services Up until now all our applications have

consumed services provided by other people

Now we are going to learn how to create services of our own and consume them on the Windows Phone

We are going to use the Windows Communications Framework (WCF) to do this

Page 4: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone4

Services and proxies

The service infrastructure hides the nature of the network connection from both the server and the client The server contains methods that are

called to provide the service The client calls methods on a proxy

object that represents the service

Server

Service Proxy Object

Page 5: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone5

Creating a Service

A service is a Visual Studio project like any other

It can run in a test environment on the development machine

Page 6: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone6

The “Joke of the day” service

The “Joke of the day” service contains a single method that accepts an integer and returns a string containing a joke of that “strength”

[ServiceContract]public interface IJokeOfTheDayService{

[OperationContract] string GetJoke(int jokeStrength);

}

Page 7: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone7

Contract attributes

The [ServiceContract] and [OperationContract] attributes are used by the build process to generate the service descriptions

[ServiceContract]public interface IJokeOfTheDayService{

[OperationContract] string GetJoke(int jokeStrength);

}

Page 8: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone8

The “Joke of the day” method

The [ServiceContract] and [OperationContract] attributes are used by the build process to generate the service descriptions

public class JokeOfTheDayService : IJokeOfTheDayService{ public string GetJoke(int jokeStrength) { string result = "Invalid strength"; switch (jokeStrength) { case 0: result = "Joke 0 text"; break; case 1: result = "Joke 1 text"; break; case 2: result = "Joke 2 text"; break; } return result; }}

Page 9: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone9

Joke of the day service

We can test the service by issuing calls on the methods from the WCF Test Client

This runs as part of the service project

Page 10: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone10

Joke of the day service description

The service also provides a service description that can be used to create clients that use it

Page 11: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone11

Creating a Service Reference A service reference is

added alongside dll references that a project uses

It will be added to the Visual Studio project

We manage the service properties from Solution Explorer and the Properties Pane

Page 12: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone12

Browsing for a service

We enter the address of the service and Visual Studio downloads the service description

Page 13: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone13

Service reference management Once a service has been added it appears in the project

A given project can connect to multiple services

Page 14: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone14

Making a proxy object

The proxy object provides the link between the client application and the service providing the resource

JokeOfTheDayService.JokeOfTheDayServiceClient jokeService; // Constructorpublic MainPage(){ InitializeComponent();

jokeService = new JokeOfTheDayService.JokeOfTheDayServiceClient();

jokeService.GetJokeCompleted +=

new EventHandler<JokeOfTheDayService.

GetJokeCompletedEventArgs> (jokeService_GetJokeCompleted);}

Page 15: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone15

Making a proxy object

The proxy object provides the link between the client application and the service providing the resource

JokeOfTheDayService.JokeOfTheDayServiceClient jokeService; // Constructorpublic MainPage(){ InitializeComponent();

jokeService = new JokeOfTheDayService.JokeOfTheDayServiceClient();

jokeService.GetJokeCompleted +=

new EventHandler<JokeOfTheDayService.

GetJokeCompletedEventArgs> (jokeService_GetJokeCompleted);}

Create the service

Page 16: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone16

Making a proxy object

The proxy object provides the link between the client application and the service providing the resource

JokeOfTheDayService.JokeOfTheDayServiceClient jokeService; // Constructorpublic MainPage(){ InitializeComponent();

jokeService = new JokeOfTheDayService.JokeOfTheDayServiceClient();

jokeService.GetJokeCompleted +=

new EventHandler<JokeOfTheDayService.

GetJokeCompletedEventArgs> (jokeService_GetJokeCompleted);}

Bind to the service completed event

Page 17: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone17

Asynchronous service calls Like every other network mechanism,

requests to web services are asynchronous The foreground program sends off the

service request When the service completes it fires an

event in the program We have to bind an event handler to

the service completed message This will display our joke

Page 18: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone18

Displaying the result

This method checks the return arguments to make sure that the call has succeeded

If it has, the joke is displayed in a TextBox

void jokeService_GetJokeCompleted(object sender, JokeOfTheDayService.GetJokeCompletedEventArgs e){ if (!e.Cancelled) { jokeTextBlock.Text = e.Result; }}

Page 19: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone19

Sending the request

When the button is clicked it loads the requested strength and calls the method on the instance of the service

private void getJokeButton_Click(object sender, RoutedEventArgs e){ int strength = 0;

if (int.TryParse(strengthTextBox.Text, out strength)) { jokeService.GetJokeAsync(strength); }}

Page 20: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone20

Errors and Timeouts

Network connections are not guaranteed

It may be impossible to make a connection to a service

Your application should handle this

Page 21: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone21

Demo 1: Joke Service

Demo

Page 22: 3 4 5 6 [ServiceContract] public interface IJokeOfTheDayService { [OperationContract] string GetJoke(int jokeStrength); }

Windows Phone22

Review Windows Phone devices can act as

clients to servers running C# programs that provide services

The network abstracts the client request into a method call in a proxy object

A service exposes a description that can be used by Visual Studio to create appropriate proxy objects