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.

No comments: