3 4 5 6 [servicecontract] public interface ijokeofthedayservice { [operationcontract] string...
TRANSCRIPT
7
Session 5.3
Using Network Services
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
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
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
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
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);
}
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);
}
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; }}
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
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
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
Windows Phone12
Browsing for a service
We enter the address of the service and Visual Studio downloads the service description
Windows Phone13
Service reference management Once a service has been added it appears in the project
A given project can connect to multiple services
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);}
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
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
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
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; }}
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); }}
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
Windows Phone21
Demo 1: Joke Service
Demo
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