Property Value
Address net.tcp://localhost:1234/MessageService
Binding netTcpBinding

Figure 20-32

Save the file and close the editor.

Basically, you set the endpoint to use the netTcpbinding binding. Examine the App.config file now, and you'll see that the following highlighted code has been added:

<?xml version='1.0' encoding='utf-8'?>

<configuration>

 ...

  <services>

   <service

    behaviorConfiguration='MessageServer.MessageServiceBehavior'

    name='MessageServer.MessageService'>

    <endpoint

     address='net.tcp://localhost:l234/MessageService'

     binding='netTcpBinding' bindingConfiguration=''

     contract='MessageServer.IMessageService'>

     <identity>

      <dns value='localhost'/>

     </identity>

    </endpoint>

    <endpoint address='mex' binding='mexHttpBinding'

     contract='IMetadataExchange'/>

    <host>

     <baseAddresses>

      <add

       baseAddress='http://localhost:8731/Design_Time_Addresses/MessageServer/MessageService/'/& gt;

     </baseAddresses>

    </host>

   </service>

  </services>

 </system.serviceModel>

</configuration>

Notice the base address contained in the app.config file:

http://localhost:8731/Design_Time_Addresses/MessageServer/MessageService/

This is the address that clients can use to add a service reference to your WCF service.

Press F5 to test the application now. When prompted with the Windows Security Alert dialog, click Unblock (see Figure 20-33).

Figure 20-33

In this example, the WCF service is hosted by the Windows Form application, at port 1234, using the TCP protocol.

Creating the Client

Launch another instance of Visual Studio 2008, and create a new Windows Forms Application project. Name it MessageClient.

Populate the default Form1 with the controls shown in Figure 20-34.

Figure 20-34

Add a service reference to the WCF service created earlier (see Figure 20-35). Enter the base address URL (http://localhost:8731/Design_Time_Addresses/MessageServer/MessageService) that you have observed in the app.config file.

Figure 20-35

Switch to the code-behind of Form1, and import the following namespace:

using System.ServiceModel;

Declare the following member variable:

public partial class Form1 : Form {

 ServiceReferencel.MessageServiceClient client;

Double-click the Send button, and code the button1_Click event handler as follows:

private void btnSend_Click(object sender, EventArgs e) {

 client = new

  MessageClient.ServiceReference1.MessageServiceClient ();

 client.SetMessage(textBox1.Text);

 client.Close();

}

That's it! Press F5 and you can now send a message to the server using the WCF service (see Figure 20- 36).

Figure 20-36

Implementing WCF Callbacks

One of the limitations of a traditional ASMX Web Service call lies in its request/response communication model. ASMX Web Services calls are passive and return results only when called upon. For instance, say that a particular cinema operator deploys a Web Service to allow online purchasing of tickets. The cinema's branches have systems that are connected to the Web Service to obtain the latest status of seat allocation and that sell tickets to cinema goers. In this case, the systems have to keep polling the Web Service at regular intervals to obtain the latest seats status. Moreover, it is very likely that a few branches may be booking the same seat(s) at the same time.

A better approach would be for the Web Service to notify all the branches about the changes in seat status as and when a seat has been reserved. This way, all branches have the latest seat information, and there is no

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату