Likes

Developing Windows Azure and Web Services Lab @ 7 Ans 2

The first step in creating a WCF service is to define the service contract and data contracts. Only afterwards you can begin implementing the service contract. In this exercise, you will define a service contract interface for the booking service along with the required data contracts, and then you will implement the service contract.
The main tasks for this exercise are:
1. Create a data contract for the booking request.
2. Create a service contract for the booking service.
3. Implement the service contract.



To create a data contract for the booking request, you need to perform the following steps:
1. Browse to the location where the Exercise 01 .zip file is saved.
2. Extract the files.
3. Double-click the Exercise 01 folder.
4. Double-click the BlueYonder.Server folder.
5. Double-click the BlueYonder.Server.sln file. The BlueYonder.Server - Microsoft Visual Studio window is displayed.
6. Ensure that the Solution Explorer window is opened.
7. Right-click the BlueYonder.BookingService.Contracts node, and then select Add Reference. The Reference Manager - BlueYonder.BookingService.Contracts dialog box is displayed.
8. Ensure that the Assemblies node is expanded in the left pane.
9. Ensure that the Framework node is selected.
10. Scroll down and select the check boxes next to the following assembly names in the middle pane:
o System.Runtime.Serialization * System. ServiceModel
11. Click the OK button.
12. Right-click the BlueYonder.BookingService.Contracts node, and then select Add—Class. The Add New Item -BlueYonder.BookingService.Contracts dialog box is displayed.
13. Ensure that Class is selected in the middle pane.
14. Select and replace the existing text in the Name text box with TripDto.
15. Click the Add button. The TripDto.cs file is displayed.
16. Type the highlighted portions of the following code snippet in the TripDto.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using BlueYonder.Entities;
namespace BlueYonder.BookingService.Contracts
{
[DataContract] public class TripDto {
[DataMember]
public int FlightSchedulelD { get; set; }
[DataMember]
public FlightStatus Status { get; set; }
[DataMember]
public SeatClass Class { get; set; }
}
}
17. Select FILE—Save All to save the changes.
18. Ensure that the Solution Explorer window is opened.
19. Right-click the BlueYonder.BookingService.Contracts node, and then select Add—Class. The Add New Item -BlueYonder.BookingService.Contracts dialog box is displayed.
20. Ensure that Class is selected in the middle pane.
21. Select and replace the existing text in the Name text box with ReservationDto.
22. Click the Add button. The ReservationDto.es file is displayed.
23. Type the highlighted portions of the following code snippet in the ReservationDto.es file: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace BlueYonder.BookingService.Contracts
{
[OataContract] public class ReservationDto
{
[DataMember]
public int Travelerld { get; set; }
[DataMember]
public DateTime ReservationDate { get; set; }
[DataMember]
public TripDto DepartureFlight { get; set; }
[DataMember]
public TripDto ReturnFlight { get; set; }
}
}
24. Select FILE— Save All to save the changes.
25. Ensure that the Solution Explorer window is opened.
26. Right-click the BlueYonder.Booking Service.Contracts node, and then select Add—New Folder.
27. Replace the existing text of the folder name with Faults, and then press the Enter key.
28. Right-click the Faults folder, and then select Add—Class. The Add New Item -BlueYonder.Booking Service.Contracts dialog box is displayed.
29. Ensure that Class is selected in the middle pane.
30. Select and replace the existing text in the Name text box with ReservationCreationFault.
31. Click the Add button. The ReservationCreationFault.es file is displayed.
32. Type the highlighted portions of the following code snippet in the ReservationCreationFault.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace BlueYonder.BookingService.Contracts.Faults
{
[DataContract]
public class ReservationCreationFault
{
[DataMember]
public string Description { get; set; }
[DataMember]
public DateTime ReservationDate { get; set; }
}
}
33. Select FILE— Save All to save the changes.
Task 2: Creating a Service Contract for the Booking Service
To create a service contract for the booking service, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Right-click the BlueYonder.Booking Service.Contracts node, and then select Add—New Item. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
3. Select Interface in the middle pane.
4. Select and replace the existing text in the Name text box with IBooking Service.
5. Click the Add button. The IBooking Service.cs file is displayed.
6. Type the highlighted portions of the following code snippet in the IBooking Service.cs file:
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel;
using BlueYonder.BookingService.Contracts.Faults;
namespace BlueYonder.BookingService.Contracts
{
[ServiceContract(Namespace = "http://blueyonder.server.interfaces/")] public interface IBookingService {
[OperationContract]
[FaultContract(typeof(ReservationCreationFault))] string CreateReservation(ReservationDto request);
}
}
7. Select FILE— Save All to save the changes.
Task 3: Implementing the Service Contract
To implement the service contract, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Right-click the BlueYonder.Booking Service.lmplementation node, and then select Add Reference. The Reference Manager - BlueYonder.Booking Service.lmplementation dialog box is displayed.
3. Ensure that the Framework node is selected in the left pane.
4. Scroll down and select the check box next to the System. ServiceModel assembly in the middle pane.
5. Click the OK button.
6. Right-click the BlueYonder.Booking Service.lmplementation node, and then select Add—Class. The Add New Item -BlueYonder.BookingService.Implementation dialog box is displayed.
7. Ensure that Class is selected in the middle pane.
8. Select and replace the existing text in the Name text box with Booking Service.
9. Click the Add button. The Booking Service.cs file is displayed.
10. Type the highlighted portions of the following code snippet in the BookingService.es file: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using BlueYonder.BookingService.Contracts;
using BlueYonder.BookingService.Contracts.Faults;
using BlueYonder.DataAccess.Interfaces;
using BlueYonder.DataAccess.Repositories;
using BlueYonder.Entities;
namespace BlueYonder.BookingService.Implementation
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class BookingService : IBookingService
{
}
}
11. Add the following code snippet inside the Bockingservice class:
public static readonly string ConnectionName = "BlueYonderServer"; public string CreateReservation(ReservationDto request)
{
if (request.DepartureFlight == null)
{
throw new FaultException<ReservationCreationFault>( new ReservationCreationFault {
Description = "Reservation must include a departure flight", ReservationDate = request.ReservationDate }, "Invalid flight info");
}
var reservation - new Reservation
{
Travelerld = request.Travelerld,
ReservationDate - request.ReservationDate,
DepartureFlight = new Trip
{
Class - request.DepartureFlight.Class,
Status = request.DepartureFlight.Status,
FlightSchedulelD - request.DepartureFlight.FlightSchedulelD
}
};
if (request.ReturnFlight !- null)
{
reservation.ReturnFlight - new Trip
{
Class = request.ReturnFlight.Class,
Status = request.ReturnFlight.Status,
FlightSchedulelD = request.ReturnFlight.FlightSchedulelD

}
using (IReservationRepository reservationRepository ■ new ReservationRepository(ConnectionName))
{
reservation.ConfirmationCode -
ReservationUtils.GenerateConfirmationCode(reservationRepository); reservationRepository.Add(reservation); reservationRepository. SaveQ; return reservation.ConfirmationCode;
}
}
12. Right-click the first line of code in the CreateReservation method, and then select Breakpoint—Insert Breakpoint.
13. Select FILE—Save All to save the changes.
14. Press the F6 key. and ensure that the Build successed message is displayed on the status bar.
15. Close Microsoft Visual Studio 2012.

Developing Windows Azure and Web Services Lab @ 7 Ans 1

To support secured communication between the service and the client you will need to define a certificate.
You need to add to the service support for HTTPS binding that uses a certificate and set the client to work with the secured connection.
The main tasks for this exercise are:
1. Add an HTTPS binding in IIS.
2. Host ASP.NET Web API Web application in IIS.
3. Change the addresses of the service used in the client application and run the client application.




To implement the required functionality, you need to perform the following steps:
1. Browse to the location where the Exercise 05.zip file is saved.
2. Extract the Exercise 05.zip file.
3. Double-click the Exercise 05 folder.
4. Press the Window logo key. The Start screen is displayed.
5. Start hyping Internet Information Services.
6. Click the Internet Information Services (IIS) Manager tile. The Internet Information Services (IIS) Manager window is displayed.
7. Click the No button, if the Internet Information Services (IIS) Manager dialog box is displayed.
8. Double-click the Server Certificates icon under the IIS group in the middle pane.
9. Click the Create Self-Signed Certificate link in the Actions pane. The Create Self-Signed Certificate dialog box is displayed.
10. Type BlueYonderAirlinesRootCA in the Specify a friendly name for the certificate text box.
11. Select the Web Hosting option from the Select a certificate store for the new certificate drop-down list.
12. Click the OK button.
13. Expand the node in the Connections pane that has same name as your machine name.
14. Expand the Sites node.
15. Select the Default Web Site node.
16. Click the Bindings link in the Actions pane. The Site Bindings dialog box is displayed.
17. Click the Add button. The Add Site Binding dialog box is displayed.
18. Select the https option from the Type drop-down list.
19. Type localhost in the Host name text box.
20. Select the BlueYonderAirlinesRootCA option from the SSL certificate drop-down list.
21. Click the OK button. An HTTPS binding is added to the Site Binding list.
22. Click the Close button to close the Site Bindings dialog box. When you add HTTPS binding to the Site Binding, all Web applications that are configured with HTTP binding will support HTTPS.
23. Open Microsoft Visual Studio 2012 as administrator.
24. Select FILE—Open—Project-Solution. The Open Project dialog box is displayed.
25. Browse to the location where the Exercise 05.zip file is extracted.
26. Double-click the Exercise 05 folder.
27. Double-click the BlueYonder.Companion folder.
28. Select the BlueYonder.Companion.sln file.
29. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio (Administrator) window is displayed.
30. Ensure that the Solution Explorer window is opened.
31. Ensure that the BlueYonder.Companion.Host node is expanded.
32. Double-click the Web.config file. The Web.config file is displayed.
33. Locate and replace the string user id=BlueYonder;Password=Pa$$werd; with user id=sa;Password=passwcrdSl23;.
34. Select BUILD—Batch Build. The Batch Build dialog box is displayed.
35. Click the Select All button.
36. Click the Clean button. The Batch Build dialog box is closed automatically.
37. Select BUILD—Batch Build. The Batch Build dialog box is displayed.
38. Ensure that all the check boxes are selected under the Build column.
39. Click the Build button. The Batch Build dialog box is closed automatically.
Note: If errors or warnings are displayed in the build process, right-click the root solution node and then select Manage NuGet Packages for Solution.
Further, click the Restore button in the popup that appears at the top of the dialog box displayed. Once all the references are restored, close the dialog box and repeat the build process.
40. Ensure that the Solution Explorer window is opened.
41. Right-click the BlueYonder.Companion.Host node, and then select Properties The BlueYonder.Companion.Host window is displayed.
42. Select Web in the left pane.
43. Clear the Use IIS Express check box.
44. Click the Create Virtual Directory button. If the Microsoft Visual Studio dialog box appears, click the Yes button.
45. Click the OK button when the confirmation message box is displayed.
46. Select FILE—Save All to save the changes.
47. Switch to the Internet Information Services (IIS) Manager window.
48. Right-click the Default Web Site node, and then select Refresh.
49. Expand the Default Web Site node.
50. Click the BlueYonder.Companion.Host node.
51. Click the Browse '.80 (http) link in the Actions pane. The Internet Explorer window is displayed.
52. Append locations to the address in the address bar. and then press the Enter key. A prompt at the bottom of the Internet Explorer window is displayed.
53. Click the Open button.
54. Select Notepad from the list of available programs, if you are prompted to select a program to open the file. The locations.json file is displayed.
55. Close the locations.json file.
56. Replace the address available in the address bar of the Internet Explorer window with https://localhost/BlueYonder.Companion.Host-locations. and then press the Enter key.
57. Click the Continue to this website (not recommended), link, if you get a message There is a problem with this website's security certificate. A prompt at the bottom of the Internet Explorer window is displayed.
58. Click the Open button.
59. Select Notepad from the list of available programs if you are prompted to select a program to open the file. The locations.json file is displayed.
60. Explore the contents of the file. It contains data of the Location table.
61. Close the locations.json file.
62. Close the Internet Information Services (IIS) Manager window.
63. Close the Internet Explorer window.
64. Switch to Microsoft Visual Studio 2012, and close it.

Developing Windows Azure and Web Services Lab @ 6 Ans 2

To make your web application more robust, you need to add some validations to the incoming data. You will apply validation rule to your server to verify that all required fields of a model are sent from the client before handling the request. You will decorate the Travel model with attributes that define the required fields, then you will derive from the ActionFilter class and implement the validation of the model. Finally, you will add the validation to the Post action.
The main tasks for this exercise are:
1. Add data annotations to the Traveler class.
2. Create a class that derives from ActionFilterAttribute.
3. Implement the action filter to validate the model.
4. Add a custom attribute to the put and post actions in the booking service.




To implement the required functionality, you need to perform the following steps:
1. Browse to the location where the Exercise 04.zip file is saved.
2. Extract the files.
3. Press the Window logo key. The Start screen is displayed.
4. Start typing Visual Studio 2012.
5. Right-click theVisual Studio 2012 tile. The App bar is displayed.
6. Click the Run as administrator button.
7. Select FILE—Open—Project/Solution. The Open Project dialog box is displayed.
8. Browse to the location where the Exercise 04.zip file is extracted.
9. Double-click the Exercise 04 folder.
10. Double-click the BlueYonder.Companion folder.
11. Select the BlueYonder.Companion.sln file.
12. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio (Administrator) window is displayed.
13. Ensure that the Solution Explorer window is opened.
14. Expand the BlueYonder.Entities node.
15. Double-click the Traveler.es file. The Traveler.es file is displayed.
16. Type the highlighted portions of the following code snippet in the Traveler.es file:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace BlueYonder.Entities
{
public class Traveler
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Travelerld { get; set; }
public string TravelerUserldentity { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Phone]
public string MobilePhone { get; set; }
[Required]
public string HomeAddress { get; set; }
public string Passport { get; set; }
[EmailAddress]
public string Email { get; set; }
>
}
17. Select FILE—Save All to save the changes.
18. Right-click the BlueYonder.Companion.Controllers project in the Solution Explorer window, and then select Add—New Folder.
19. Type ActionFilters. and then press the Enter key.
20. Right-click the ActionFilters folder, and then select Add—Class. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
21. Select and replace the existing text in the Name text box with ModelValidationAttribute.
22. Click the Add button. The ModelValidationAttribute.es file is displayed.
23. Type the highlighted portions of the following code snippet in the ModelValidationAttribute.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Filters;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace BlueYonder.Companion.Controllers.ActionFilters
{
public class ModelValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(
System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModclStatc.IsValid)
actionContext.Response = actionContext.Request.CreateErrorRespons HttpStatusCode.BadRequest, new HttpError(actionContext.ModelState, true));
>
}
}
24. Select FILE— Save All to save the changes.
25. Ensure that the BlueYonder.Companion.Controllers node is expanded in the Solution Explorer window.
26. Double-click the TravelersController.es file. The TravelersController.es file is displayed.
27. Type the highlighted portion of the following code snippet in the TravelersController.es file:
using BlueYonder.Companion.Entities; using BlueYonder.DataAccess.Repositories; using BlueYonde r.Companion.Controlle rs.ActionF ilters; namespace BlueYonder.Companion.Controllers
28. Type the highlighted portion of the following code snippet in the TravelersController.es file: public HttpResponseMessage Get(string id)
{
var traveler = Travelers.FindBy( t -> t .Travelerllserldentity -- id) . FirstOrDefaultQ;
// Handling the HTTP status codes
if (traveler != null)
return Request.CreateResponse<Traveler>
(HttpStatusCode.OK, traveler); else
return Request.CreateResponse(HttpStatusCode.NotFound);
}
[ModelValidation]
public HttpResponseMessage Post(Traveler traveler)
{
// saving the new order to the database Travelers.Add(traveler);
Travelers.Save();
// creating the response, with three key features:
// 1. the newly saved entity // 2. 201 Created status code
// 3. Location header with the location of the new resource var response ■ Request.CreateResponse(HttpStatusCode.Created, traveler); response.Headers.Location = new Uri(Request.RequestUri, traveler.Traveler ToStringQ);
return response;
}
[ModelValidation]
public HttpResponseMessage Put(int id, [FromBody]Traveler traveler)
{
// returning 404 if the entity doesn't exist if (Travelers.GetSingle(id) == null)
return Request.CreateResponse(HttpStatusCode.NotFound);
T ravelers.Edit(traveler);
T ravelers.Save();
return Request.CreateResponse(HttpStatusCode.OK);
}
29. Select FILE—Save All to save the changes.
30. Select BUILD—Build Solution. The Build succeeded message is displayed on the status bar at the bottom of the BlueYonder.Companion - Microsoft Visual Studio (Administrator) window.
31. Close Microsoft Visual Studio 2012.

Developing Windows Azure and Web Services Lab @ 6 Ans 1

OData is a data access protocol that provides standard CRUD access of a data source via a website. To add support for OData protocol you will install a NuGet Microsoft.AspNet.WebApi.OData package, and then decorate the methods that you want to support OData with [Queryable] attribute.
The main tasks for this exercise are:
1. Add a queryable action to the flight schedule service.
2. Handle the search event in the client application and query the flight schedule service by using OData filters.




Task 1: Adding a queryable action to the flight schedule service
To add a queryable action to the flight schedule service, you need to perform the following steps:
1. Browse to the location where the Exercise 03.zip file is saved.
2. Extract the files.
3. Press the Windows logo key. The Start screen is displayed.
4. Start typing Visual Studio 2012. The Search pane and Apps screen are displayed.
5. Right-click the Visual Studio 2012 tile, and then select Run as Administrator.
6. Select FILE—Open—Project-Solution. The Open Project dialog box is displayed.
7. Browse to the location where the Exercise 03 folder is extracted.
8. Double-click the Exercise 03 folder.
9. Double-click the BlueYonder.Companion folder.
10. Select the BlueYonder.Companion.sin file.
11. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio window is displayed.
12. Ensure that the Solution Explorer window is opened.
13. Click the TOOL S menu on the toolbar, and then select Library Package Manager—Package Manager Console. The Package Manager Console window is displayed.
14. Type install-package Microsoft.AspNet.WebApi.OData -version 0.1.0-alpha-120815 -ProjectName BlueYonder.Companion.Controllers, and then press the Enter key.
15. Wait until Package Manager Console finished downloading and adding the package.
16. Ensure that the Solution Explorer window is opened.
17. Expand the BlueYonder.Companion.Controllers node.
18. Double-click the LocationsController.es file. The LocationsController.es file is displayed.
19. Replace the existing Get? {) method, which has the three parameters with the following code snippet in the LocationsController.es file:
[Queryable]
public IQueryable<Location> Get()
{
return Locations.GetAll();
}
20. Select FILE—Save All to save the changes.
21. Close Microsoft Visual Studio 2012.
Task 2: Handling the Search Event in the Client Application and Query the Flight Schedule Service by Using OData filters
To handle the search event in the client application and query the flight schedule service by Using OData filters, you need to perform the following steps:
1. Press the Windows logo key. The Start screen is displayed.
2. Start hyping Visual Studio 2012. The Search pane and Apps screen are displayed.
3. Right-click the Visual Studio 2012 tile, and then select Run as Administrator.
4. Select FILE—Open—Projecb'Solution. The Open Project dialog box is displayed.
5. Browse to the location where the Exercise 03 folder is extracted.
6. Double-click the Exercise 03 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. Ensure that Solution Explorer window is opened.
11. Expand the BlueYonder.Comapanion.Shared node.
12. Double-click the Addresses.es file. The Addresses.es file is displayed.
13. Replace the return statement of the GetLocationsWithQueryUri property with the following code snippet:
return GetLocationsUri + M?Sfilter=substringof(tolower(,{0},),tolower(City))";
14. Select FILE— Save All to save the changes.
15. Close Microsoft Visual Studio 2012.

Developing Windows Azure and Web Services Lab @ 5 Ans 2

To support RSS feeds of flight schedule you will create a new media type that handles the RSS requests, and then register the new media formatter class in the Http Configuration formatters.
The main tasks for this exercise are:
1. Create a new class that derives from MediaTypeFormatter.
2. Implement the necessary code to return an RSS response containing flight schedule information.
3. Register the new media formatter.
4. Use Internet Explorer to view the feed.



Task 1: Creating a New Class that Derives from MediaTypeFormatter
To create a new class that derives from MediaTypeFormatter. 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. Press the Windows logo key. The Start screen is displayed.
4. Start typing Visual Studio 2012.
5. Right-click the Visual Studio 2012 tile.The App bar is displayed.
6. Click the Run as administrator button.
7. Select FILE—Open—Project-Solution. The Open Project dialog box is displayed.
8. Browse to the location where the Exercise 02 folder is extracted.
9. Double-click the Exercise 02 folder.
10. Double-click the BlueYonder.Companion folder.
11. Select the BlueYonder.Companion.sin file.
12. Click the Open button. The BlueYonder.Companion - Microsoft Visual Studio (Administrator) window is displayed.
13. Ensure that the Solution Explorer window is opened.
14. Right-click the BlueYonder.Companion.Controllers node, and then select Add—New Folder.
15. Replace the existing text of the folder name with Formatters, and then press the Enter key.
16. Right-click the Formatters folder, and then select Add—Class. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
17. Select and replace the existing text in the Name text box with Atom Formatter.
18. Click the Add button. The AtomFormatter.es file is displayed.
19. Type the highlighted portions of the following code snippet in the AtomFormatter.es file:
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml;
using BlueYonder.Companion.Entities; using System.Net.Http; using System.Net.Http.Formatting; using System.Net.Http.Headers; using System.ServiceModel.Syndication;
namespace BlueYonder.Companion.Controllers.Formatters
{
public class AtomFormatter : MediaTypeFormatter
{
}
}
20. Select FILE— Save All to save the changes.
Task 2: Implementing the Necessary Code to Return RSS Response Containing Flight Schedule Information
To implement the necessary code to return RSS response containing flight schedule information, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Ensure that the AtomFormatter.es file is opened.
3. Type the highlighted portions of the following code snippet in the AtomFormatter.es file:
class AtomFormatter : MediaTypeFormatter
{
private HttpRequestMessage request;
//Add the new media type header to the supported media types, public AtomFormatterQ {
SupportedMediaTypes.Add(new MediaTypeHeaderValue ("application/atom+xml") );
>
//Is used to receive the instance of the HttpRequestMessage class, public AtomFormatter(HttpRequestMessage request)
{
_request - request;
}
//Overrides the CanReadType method, to define it is not supporting deseriali public override bool CanReadType(Type type)
{
return false;
}
//Overrides the CanWriteType method, to define it supports serialization of FlightWithSchedulesDTO class instances.
public override bool CanWriteType(Type type)
{
>
return type == typeof(FlightWithSchedulesDTO);
private SyndicationFeed CreateFeed(FlightWithSchedulesDTO flight)
{
var feed = new SyndicationFeed
{
Title = new TextSyndicationContent(string.Format("Blue Yonder flight {0}", flight.FlightNumber))
};
var items = from s in flight.Schedules select new Syndicationltem
{
Title = new TextSyndicationContent(
String.Format("Flight {0} {1}", flight.FlightNumber, s.Departure.ToString(”MMMM dd, yyyy")))>
Id = flight.FlightNumber,
BaseUri = new Uri(_request.RequestUri, string.Format("{0}/{1}",
_request.RequestUri.AbsolutePath, flight.FlightNumber)),
>;
feed.Items - items; return feed;
}
public override Task WriteToStreamAsync(Type type, object value, System.10 writeStream, RttpContent content, System.Net.TransportContext transportContext)
{
// creating a System.ServiceModel.Syndication.SyndicationFeed var feed - CreateFeed(value as FlightWithSchedulesDTO);
return Task.Run(() =>
{
using (var writer = XmlWriter.Create(writeStream))
{
Atoml0FeedFormatter atomformatter = new Atoml0FeedFormatter(fe< atomformatter.WriteTo(writer);
}
»;
}
//Overrides the base GetPerRequestFormatterlnstance method, to create a nei instance of the AtomFormatter class for each atom request.
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type typ< HttpRequestMessage request,
MediaTypeHeaderValue mediaType)
{
return new AtomFormatter(request);
}
}
}
4. Select FILE— Save All to save the changes.
Task 3: Registering the New Media Formatter
ro register the new media formatter, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Expand the BlueYonder.Companion.Host node.
3. Expand the App_Start folder.
4. Double-click the WebApiConfig.es file. The WebApiConfig.es file is displayed.
5. Type the highlighted portions of the following code snippet in the WebApiConfig.es file:
using System.Web.Http.Routing; using System.Net.Http;
using BlueYonder.Companion.Controllers.Formatters;
namespace BlueYonder.Companion.Host
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Add(new AtomFormatterQ); config.DependencyResolver ■ new BlueYonderResolverQ; config.MessageHandlers.Add(new AtomHandler()>;
6. Select FILE— Save All to save the changes.
Task 4: Using Internet Explorer to View the Feed
To use Internet Explorer to view the feed, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Ensure that the BlueYonder.Companion.Host project is set as startup project.
3. Press the F5 key. The Internet Explorer window is displayed.
4. Append atom/flights/1 to the address in the address bar. and then press the Enter key. The details of a flight are
5. Switch to Microsoft Visual Studio 2012.
6. Press the Shift+F5 keys.
7. Close Microsoft Visual Studio 2012.

Developing Windows Azure and Web Services Lab @ 5 Ans 1

The first step in creating a WCF service is to define the service contract and data contracts. Only afterwards you can begin implementing the serv ice contract. In this exercise, you will define a service contract niter face for the booking service along with the required data contracts, and then you will implement the service contract.
The main tasks for this exercise are:
1. Create a data contract for the booking request.
2. Create a service contract for the booking service.
3. Implement the service contract.




To implement the required functionality, you need to perform the following tasks:
1. Create a data contract for the booking request.
2. Create a service contract for the booking service.
3. Implement the service contract.
Task 1: Creating a Data Contract for the Booking Request
To create a data contract for the booking request, you need to perform the following steps:
1. Browse to the location where the Exercise 01 .zip file is saved.
2. Extract the files.
3. Double-click the Exercise 01 folder.
4. Double-click the BlueYonder.Server folder.
5. Double-click the BlueYonder.Server.sln file. The BlueYonder.Server - Microsoft Visual Studio window is displayed.
6. Ensure that the Solution Explorer window is opened.
7. Right-click the BlueYonder.BookingService.Contracts node, and then select Add Reference. The Reference Manager - BlueYonder.BookingService.Contracts dialog box is displayed.
8. Ensure that the Assemblies node is expanded in the left pane.
9. Ensure that the Framework node is selected.
10. Scroll down and select the check boxes next to the following assembly names in the middle pane:
o System.Runtime.Serialization * System. ServiceModel
11. Click the OK button.
12. Right-click the BlueYonder.BookingService.Contracts node, and then select Add—Class. The Add New Item -BlueYonder.BookingService.Contracts dialog box is displayed.
13. Ensure that Class is selected in the middle pane.
14. Select and replace the existing text in the Name text box with TripDto.
15. Click the Add button. The TripDto.cs file is displayed.
16. Type the highlighted portions of the following code snippet in the TripDto.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using BlueYonder.Entities;
namespace BlueYonder.BookingService.Contracts
{
[DataContract] public class TripDto {
[DataMember]
public int FlightSchedulelD { get; set; }
[DataMember]
public FlightStatus Status { get; set; }
[DataMember]
public SeatClass Class { get; set; }
>
>
17. Select FILE—Save All to save the changes.
18. Ensure that the Solution Explorer window is opened.
19. Right-click the BlueYonder.BookingService.Contracts node, and then select Add—Class. The Add New Item -BlueYonder.BookingService.Contracts dialog box is displayed.
20. Ensure that Class is selected in the middle pane.
21. Select and replace the existing text in the Name text box with ReservationDto.
22. Click the Add button. The ReservationDto.es file is displayed.
23. Type the highlighted portions of the following code snippet in the ReservationDto.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace BlueYonder.BookingService.Contracts
{
[OataContract] public class ReservationDto
{
[DataMember]
public int Travelerld { get; set; }
[DataMember]
public DateTime ReservationDate { get; set; }
[DataMember]
public TripDto DepartureFlight { get; set; }
[DataMember]
public TripDto ReturnFlight { get; set; }
}
}
24. Select FILE— Save All to save the changes.
25. Ensure that the Solution Explorer window is opened.
26. Right-click the BlueYonder.Booking Service.Contracts node, and then select Add—New Folder.
27. Replace the existing text of the folder name with Faults, and then press the Enter key.
28. Right-click the Faults folder, and then select Add—Class. The Add New Item -BlueYonder.Booking Service.Contracts dialog box is displayed.
29. Ensure that Class is selected in the middle pane.
30. Select and replace the existing text in the Name text box with ReservationCreationFault.
31. Click the Add button. The ReservationCreationFault.es file is displayed.
32. Type the highlighted portions of the following code snippet in the ReservationCreationFault.es file: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace BlueYonder.BookingService.Contracts.Faults
{
[DataContract]
public class ReservationCreationFault
{
[DataMember]
public string Description { get; set; }
[DataMember]
public DateTime ReservationDate { get; set; }
}
}
33. Select FILE— Save All to save the changes.
Task 2: Creating a Service Contract for the Booking Service
To create a service contract for the booking service, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Right-click the BlueYonder.Booking Service.Contracts node, and then select Add—New Item. The Add New Item -BlueYonder.Companion.Controllers dialog box is displayed.
3. Select Interface in the middle pane.
4. Select and replace the existing text in the Name text box with IBooking Service.
5. Click the Add button. The IBooking Service.cs file is displayed.
6. Type the highlighted portions of the following code snippet in the IBooking Service.cs file:
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel;
using BlueYonder.BookingService.Contracts.Faults;
namespace BlueYonder.BookingService.Contracts
{
[ServiceContract(Namespace = "http://blueyonder.server.interfaces/")] public interface IBookingService {
[OperationContract]
[FaultContract(typeof(ReservationCreationFault))] string CreateReservation(ReservationDto request);
>
}
7. Select FILE— Save All to save the changes.
Task 3: Implementing the Service Contract
To implement the service contract, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Right-click the BlueYonder.Booking Service.lmplementation node, and then select Add Reference. The Reference Manager - BlueYonder.Booking Service.lmplementation dialog box is displayed.
3. Ensure that the Framework node is selected in the left pane.
4. Scroll down and select the check box next to the System.ServiceModel assembly in the middle pane.
5. Click the OK button.
6. Right-click the BlueYonder.Booking Service.lmplementation node, and then select Add—Class. The Add New Item -BlueYonder.BookingService.Implementation dialog box is displayed.
7. Ensure that Class is selected in the middle pane.
8. Select and replace the existing text in the Name text box with Booking Service.
9. Click the Add button. The Booking Service.cs file is displayed.
10. Type the highlighted portions of the following code snippet in the BookingService.es file: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using BlueYonder.BookingService.Contracts;
using BlueYonder.BookingService.Contracts.Faults;
using BlueYonder.DataAccess.Interfaces;
using BlueYonder.DataAccess.Repositories;
using BlueYonder.Entities;
namespace BlueYonder.BookingService.Implementation
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class BookingService : IBookingService
{
}
}
11. Add the following code snippet inside the BookingService class:
public static readonly string ConnectionName = "BlueYonderServer"; public string CreateReservation(ReservationDto request)
{
if (request.DepartureFlight == null)
{
throw new FaultException<ReservationCreationFault>( new ReservationCreationFault {
Description = "Reservation must include a departure flight", ReservationDate = request.ReservationDate }, "Invalid flight info");
}
var reservation - new Reservation
{
Travelerld = request.Travelerld,
ReservationDate - request.ReservationDate,
DepartureFlight = new Trip
{
Class - request.DepartureFlight.Class,
Status = request.DepartureFlight.Status,
FlightSchedulelD - request.DepartureFlight.FlightSchedulelD
}
};
if (request.ReturnFlight !- null)
{
reservation.ReturnFlight - new Trip
{
Class = request.ReturnFlight.Class,
Status ■ request.ReturnFlight.Status,
FlightSchedulelD = request.ReturnFlight.FlightSchedulelD
};
}

using (IReservationRepository reservationRepository - new ReservationRepository(ConnectionName))
{
reservation.ConfirmationCode -
ReservationUtils.GenerateConfirmationCode(reservationRepository); reservationRepository.Add(reservation); reservationRepository. Save(); return reservation.ConfirmationCode;
}
}
12. Right-click the first line of code in the CreateReservation method, and then select Breakpoint—Insert Breakpoint.
13. Select FILE—Save All to save the changes.
14. Press the F6 key. and ensure that the Build successed message is displayed on the status bar.
15. Close Microsoft Visual Studio 2012.