Consume the travelers' service from the client application. Start by implementing the GetTravelerAsync method by invoking a GET request to retrieve a specific traveler from the server. Continue by implementing the CreateTravelerAsync method by invoking a POST request to create a new traveler. And complete the exercise by implementing the UpdateTravelerAsync method by invoking a PUT request to update an existing traveler.
The main tasks for this exercise are:
1. Use System.Net.HttpCIient to get a list of destinations from the Destinations service.
2. Debug the BlueYonder.Companion.Client project.
To implement the required functionality, you need to perform the following steps:
1. Browse to the location where the Exercise 02.zip file is saved.
2. Extract the files.
3. Open Microsoft Visual Studio 2012.
4. Select FILE—Open—Project/Solution. The Open Project dialog box is displayed.
5. Browse to the location where the Exercise 02.zip file is extracted.
6. Double-click the Exercise 02 folder.
7. Double-click the BlueYonder.Companion.Client folder.
8. Select the BlueYonder.Companion.Client.sln file.
9. Click the Open button. The BlueYonder.Companion.Client - Microsoft Visual Studio window is displayed.
10. Select VIEW—Task List. The Task List window is displayed.
11. Select Comments from the Categories drop-down list in the Task List window.
12. Double-click the TODO: Exercise 2: Task 1a: implement the method by getting a traveler from the server comment under the Description column of the Task List window. The DataManager.es file is displayed.
13. Delete the highlighted portion of the following code snippet in the DataManager.es file:
/// <summary>
/// Get the traveler associated with this machine III </summary>
III <returnsx/returns>
public async Task<Traveler> GetTravelerAsync()
{
// TODO: Exercise 2: Task la: implement the method by getting a traveler from the server
throw new NotlmplementedException();
}
14. Type the highlighted portion of the following code snippet in the DataManager.es file: public async Task<Traveler> GetTravelerAsyncQ {
// TODO: Exercise 2: Task la: implement the method by getting a traveler from the server
var hardwareld = GetHardwareld();
HttpClient client = new HttpClient();
var travelersllri = string. Format("{0}travelers/{1}", BaseUri, hardwareld)
HttpResponseMessage response = await client.GetAsync(new Uri(travelersUri if (response.IsSuccessStatusCode)
{
string resultlson = await response.Content. ReadAsStringAsync();
return await IsonConvert.DeserializeObjectAsync<Traveler>(resultJson)
}
else
{
return null;
}
15. Right-click the first line of code after the comment line in the GetTravelerAsync() method, and then select Breakpoint—Insert Breakpoint.
16. Double-click the TODO: Exercise 2: Task 1b: implement the method by using the HttpCIient class to invoke a POST request sending the traveler DTO comment under the Description column of the Task List window.
17. Delete the highlighted portion of the following code snippet in the DataManager.es file:
public async Task<Traveler> CreateTravelerAsync()
{
// TODO: Exercise 2: Task lb: implement the method by using the HttpClier class to invoke a POST request sending the traveler DTO throw new NotlmplementedExceptionQ;
}
18. Type the highlighted portion of the following code snippet in the DataManager.es file: public async Task<Traveler> CreateTravelerAsyncQ
{
// TODO: Exercise 2: Task lb: implement the method by using the HttpClien class to invoke a POST request sending the traveler DTO var dto = new TravelerDTOQ {
TravelerUserldentity = GetHardwareld()
h
string json = JsonConvert.SerializeObject(dto);
HttpCIient client = new HttpClientQ; var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json" HttpResponseMessage response = await client.PostAsync(
new Uri(_createTravelerUri), content);
var resultlson = await response.Content. ReadAsSt ringAsyncQ;
return await JsonConvert.DeserializeObjectAsync<Traveler>(resultJson);
}
19. Right-click the first line of code after the comment in the CreateTravelerAsyncO method, and then select Breakpoint—Insert Breakpoint.
20. Double-click the TODO: Exercise 2: Task 1c: implement the method by using the HttpCIient class to invoke a PUT request sending the traveler DTO comment under the Description column of the Task List window.
21. Delete the highlighted portion of the following code snippet in the DataManager.es file:
/// <summary>
III Modify the traveler details III </summary>
III <param name="traveler">The Traveler</param>
III <returnsx/returns>
public async Task UpdateTravelerAsync(Traveler traveler)
{
if (lawait NetworkManager.CheckInternetConnection(true, "")) return;
// TODO: Exercise 2: Task lc: implement the method by using the HttpCIient class invoke a PUT request sending the traveler DTO throw new NotlmplementedExceptionQ;
}
22. Type the highlighted portion of the following code snippet in the DataManager.es file: public async Task UpdateTravelerAsync(Traveler traveler)
{
if (lawait NetworkManager.CheckInternetConnection(true, "")) return;
// TODO: Exercise 2: Task lc: implement the method by using the HttpClient class invoke a PUT request sending the traveler DTO var dto = traveler.ToDTO(); dto.TravelerUserldentity = GetHardwareldQ; string json = IsonConvert.SerializeObject(dto);
HttpClient client = new HttpClient(); var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json" var travelerUri = string.Format("{0}/travelers/{l}">
BaseUri, dto.TravelerUserIdentity);
await client.PutAsync(new Uri(travelerUri), content);
}
23. Right-click the first line of code in the UpdateTravelerAsync() method, and then select Breakpoint—Insert Breakpoint.
24. Locate the string http://10.10.0.10/BlueYonder.Companion.Host/ and replace it with http://localhost/BlueYonder.Companion.Host/ in the DataManager.es file.
25. Select FILE—Save All to save the changes.
26. Press the Window logo key. The Start screen is displayed.
27. Start typing Visual Studio 2012.
28. Right-click theVisual Studio 2012 tile.The App bar is displayed.
29. Click the Run as administrator button on the App bar.
30. Select FILE—Open—Project/Solution. The Open Project dialog box is displayed.
31. Browse to the location where the Exercise 02.zip file is extracted.
32. Double-click the Exercise 02 folder.
33. Double-click the BlueYonder.Companion folder.
34. Select the BlueYonder.Companion.sln file.
35. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio window is displayed.
36. Ensure that the Solution Explorer window is opened.
37. Select DEBUG—Start Debugging. The Internet Explorer window is displayed.
38. Switch to BlueYonder.Companion.Client - Microsoft Visual Studio window.
39. Select DEBUG—Start Debugging. The BlueYonder Companion app's splash screen is displayed for a few seconds, and then the code execution breaks inside the GetTravelerAsync() method. In addition, the line in breakpoint is highlighted in yellow.
40. Press the F5 key to continue. Similarly, the code execution breaks at several places where you have added the breakpoints. You need to press the F5 key to continue the execution wherever it is interrupted because of the breakpoint until the BlueYonder Companion app is launched.
41. Click the Allow button, if you are prompted to allow the app to run in the background.
42. Right-click on the blank area of the app screen. The App bar is displayed at the bottom of the screen.
43. Click the Search button on the App bar. and then type New in the Search box.
44. Click the Allow button if you are prompted to allow the app to share your location. Wait for the app to show a list of flights from Seattle to New York.
45. Click Purchase this trip. The Purchase page is displayed.
46. Type your first name in the First Name text box.
47. Type your last name in the Last Name text box.
48. Type Aa1234567 in the Passport text box.
49. Type 555-5555555 in the Mobile Phone text box.
50. Type 423 Main St. in the Home Address text box.
51. Type your email address in the Email Address text box.
52. Click the Purchase button. If the code execution breaks, press the F5 key to continue.
53. Click the Close button when the message dialog is displayed.
54. Close the BlueYonder Companion app.
55. Switch to BlueYonder.Companion - Microsoft Visual Studio window.
56. Press the Shift+F5 keys to stop debugging the application.
57. Close both the Microsoft Visual Studio 2012 windows.
No comments:
Post a Comment