220 likes | 314 Views
Using Network Services. Pongsakorn Poosankam. Microsoft Innovation Center Manager. Microsoft (Thailand ) Limited. Topics. Creating a network service Proxy objects and services Service contracts and interfaces Creating a client Adding a service reference to a project
E N D
Using Network Services Pongsakorn Poosankam Microsoft Innovation Center Manager Microsoft (Thailand) Limited
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
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
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
Creating a Service • A service is a Visual Studio project like any other • It can run in a test environment on the development machine
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]publicinterfaceIJokeOfTheDayService{ [OperationContract]stringGetJoke(intjokeStrength);}
Contract attributes • The [ServiceContract] and [OperationContract] attributes are used by the build process to generate the service descriptions [ServiceContract]publicinterfaceIJokeOfTheDayService{ [OperationContract]stringGetJoke(intjokeStrength);}
The “Joke of the day” method publicclassJokeOfTheDayService : IJokeOfTheDayService{publicstringGetJoke(intjokeStrength) {string result = "Invalid strength";switch (jokeStrength) {case0: result = "Joke 0 text";break;case1: result = "Joke 1text";break;case 2: result = "Joke 2 text";break; }return result; }} • The [ServiceContract] and [OperationContract] attributes are used by the build process to generate the service descriptions
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
Joke of the day service description • The service also provides a service description that can be used to create clients that use it
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
Browsing for a service • We enter the address of the service and Visual Studio downloads the service description
Service reference management • Once a service has been added it appears in the project • A given project can connect to multiple services
Making a proxy object • The proxy object provides the link between the client application and the service providing the resource JokeOfTheDayService.JokeOfTheDayServiceClientjokeService;// ConstructorpublicMainPage(){InitializeComponent(); jokeService = newJokeOfTheDayService.JokeOfTheDayServiceClient();jokeService.GetJokeCompleted+= newEventHandler<JokeOfTheDayService. GetJokeCompletedEventArgs> (jokeService_GetJokeCompleted);}
Making a proxy object • The proxy object provides the link between the client application and the service providing the resource JokeOfTheDayService.JokeOfTheDayServiceClientjokeService;// ConstructorpublicMainPage(){InitializeComponent(); jokeService = newJokeOfTheDayService.JokeOfTheDayServiceClient();jokeService.GetJokeCompleted+= newEventHandler<JokeOfTheDayService. GetJokeCompletedEventArgs> (jokeService_GetJokeCompleted);} Create the service
Making a proxy object • The proxy object provides the link between the client application and the service providing the resource JokeOfTheDayService.JokeOfTheDayServiceClientjokeService;// ConstructorpublicMainPage(){InitializeComponent(); jokeService = newJokeOfTheDayService.JokeOfTheDayServiceClient();jokeService.GetJokeCompleted+= newEventHandler<JokeOfTheDayService. GetJokeCompletedEventArgs> (jokeService_GetJokeCompleted);} Bind to the service completed event
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
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 voidjokeService_GetJokeCompleted(object sender, JokeOfTheDayService.GetJokeCompletedEventArgs e){if (!e.Cancelled) {jokeTextBlock.Text = e.Result; }}
Sending the request • When the button is clicked it loads the requested strength and calls the method on the instance of the service privatevoidgetJokeButton_Click(object sender,RoutedEventArgs e){int strength = 0;if (int.TryParse(strengthTextBox.Text, out strength)) {jokeService.GetJokeAsync(strength); }}
Errors and Timeouts • Network connections are not guaranteed • It may be impossible to make a connection to a service • Your application should handle this
Demo Demo 1: Joke Service
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