Likes

Developing Windows Azure and Web Services Lab @ 8 Ans 1

The second step in creating the WCF service is to create a project for hosting the service. In this exercise you will create a service host, configure it with a TCP endpoint and use it to make the service available for clients.
The main tasks for this exercise are:
1. Create a console project to host the WCF service.
2. Configure the service to use a TCP endpoint.
3. Create the service hosting code.

Solution


Task 1: Creating a Console Project to Host the WCF Service
To create a console project to host the WCF service, 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. Double-click the Exercise 02 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. Select FILE—Add—New Project. The Add New Project dialog box is displayed.
7. Select the Windows node under the Visual C# node in the left pane.
8. Select Console Application from the list of templates displayed in the middle pane.
9. Select and replace the text in the Name text box with BlueYonder.Booking Service.Host.
10. Click the OK button.
11. Ensure that the Solution Explorer window is opened.
12. Right-click the BlueYonder.Booking Service.Host node, and then select Add Reference. The Reference Manager -BlueYonder.Booking Service.Host dailog box is displayed.
13. Ensure that Framework is selected under the Assemblies node in the left pane.
14. Scroll down and select the check box next to the System.ServiceModel assembly in the middle pane.
15. Click the Solution node in the left pane.
16. Select the check boxes next to the following project names in the middle pane:
* BlueYonder.Booking Service.Contracts
o BlueYonder.Booking Service.lmplementation
o BlueYonder.DataAccess o BlueYonder.Entities
17. Click the OK button.
18. Ensure that the Solution Explorer window is opened.
19. Right-click the BlueYonder.Booking Service.Host node, and then select Manage NuGet Packages. The BlueYonder.Booking Service.Host - Manage NuGet Packages dialog box is displayed.
20. Click the Online node in the left pane.
21. Type EntityFramework in the search box on the top-right corner.
22. Select EntityFramework in the middle pane, and then click the Install button.
23. Click the I Accept button, if the License Acceptance dialog box is displayed.
24. Click the Close button.
25. Ensure that the Solution Explorer window is opened.
26. Right-click the BlueYonder.Booking Service.Host node, and then select Add—Existing Item. The Add Existing Item -BlueYonder.BookingService.Host dialog box is displayed.
27. Browse to the location where the Exercise 02.zip file is extracted.
28. Double-click the Exercise 02 folder.
29. Double-click the Assets folder.
30. Select the FlightScheduleDatabaselnitializer.es file.
31. Click the Add button
32. Select FILE— Save All to save the changes.

Task 2: Configuring the Service to Use a TCP Endpoint

To configure the service to use a TCP endpoint, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Ensure that the BlueYonder.Booking Service.Host node is expanded.
3. Double-click the App.config file. The App.config file is displayed.
4. Type the highlighted portions of the following code snippet in the App.config file:

<?xml version="l.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /'. </startup>
<system.serviceModel>
<services>
<service name="BlueYonder.BookingService.Implementation.BookingSei <endpoint name="BookingTcp" addresss"net.tcp://localhost/BlueYoi binding="netTcpBinding" contract="BlueYonder.BookingService.Contracts.Ii </service>
</services>
</system.serviceModel>
<connectionStrings>
<add name="BlueYonderServer" connectionString="Data Sources.\SQLEXPRESS$Database=BlueYonder.Server.Lab5;Integrated Security: providerName="System.Data.SqlClient" />
</connectionStrings>
5. Select FILE— Save All to save the changes.

Task 3: Creating the Service Hosting Code
To create the service hosting code, you need to perform the following steps:
1. Ensure that Solution Explorer window is opened.
2. Ensure that the BlueYonder.Booking Service.Host node is expanded.
3. Double-click the Program.es file. The Program.es file is dispalyed.
4. Type the highlighted portions of the following code snippet in the Program.es file:
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; using BlueYonder.DataAccess;
namespace BlueYonder.BookingService.Host
{
class Program
{
static void Main(string[] args)
{
}
private static void OnServiceOpened(object sender, EventArgs e)
{
Console.WriteLine('*Booking Service Is Running... Press [ENTER] to close.
>
private static void OnServiceOpening(object sender, EventArgs e)
{
Console.WriteLine(”Booking Service Is Initializing...");
}
>
>
5. Add the following code snippet inside the Main() method of the Program.es file:
van dblnitializer = new FlightScheduleDatabaselnitializer();
dblnitializer.InitializeDatabase(new TravelCompanionContext(Implementation.BookingService.ConnectionName));
van host = new ServiceHost(typeof(Implementation.BookingServ host.Opening += OnServiceOpening; host.Opened += OnServiceOpened; try {
host.Open();
>
catch (Exception e)
{
host = null;
Console.WriteLine(" *** Error occured while trying to service host *** \n\n{0}", e.Message);
Console.WriteLine("\n\n Press [ENTER] to exit.");
>
Console.ReadLine(); if (host == null) return; try {
host. Close();
}
catch (Exception)
{
host.Abort();
}
6. Select FILE—Save All to save the changes.
7. Right-click the BlueYonder.BookingService.Host node, and then select Set as Startup Project.
8. Press the F5 key. The Console window is displayed.
9. Click the Allow access button, if the Windows Security Alert dialog box is displayed.
10. Close the Console window.
11. Switch to Microsoft Visual Studio 2012, and close it.

No comments: