1 / 22

Using Network Services

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

nolen
Download Presentation

Using Network Services

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Network Services Pongsakorn Poosankam Microsoft Innovation Center Manager Microsoft (Thailand) Limited

  2. 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

  3. 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

  4. 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

  5. Creating a Service • A service is a Visual Studio project like any other • It can run in a test environment on the development machine

  6. 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);}

  7. Contract attributes • The [ServiceContract] and [OperationContract] attributes are used by the build process to generate the service descriptions [ServiceContract]publicinterfaceIJokeOfTheDayService{ [OperationContract]stringGetJoke(intjokeStrength);}

  8. 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

  9. 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

  10. Joke of the day service description • The service also provides a service description that can be used to create clients that use it

  11. 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

  12. Browsing for a service • We enter the address of the service and Visual Studio downloads the service description

  13. Service reference management • Once a service has been added it appears in the project • A given project can connect to multiple services

  14. 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);}

  15. 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

  16. 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

  17. 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

  18. 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; }}

  19. 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); }}

  20. Errors and Timeouts • Network connections are not guaranteed • It may be impossible to make a connection to a service • Your application should handle this

  21. Demo Demo 1: Joke Service

  22. 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

More Related