project.

The WCF service now has three endpoints as shown in the following table.

Name Binding Description
WS wsHttpBinding The wsHttpBinding: Uses the WS-* protocols. Security is at the message level. Uses additional handshake messaging. Supports reliable session. Messages exchanged between the client and the server are encrypted.
[Empty Name] mexHttpBinding Publishes the metadata for the WCF service, allowing clients to retrieve the metadata using a WS-Transfer GET request or an HTTP/GET request using the ? wsdl query string. By default, every WCF service created using Visual Studio 2008 has this endpoint to allow clients to request the service's metadata.
BASIC basicHttpBinding The basicHttpBinding: Supports old ASMX-style (based on WS- BasicProfile1.1) Web Services call. Does not support secure messaging (no WS enhancements). Does not support reliability and ordered delivery. Calls may be lost and the client simply time out. Calls may not be ordered correctly. Security is at the transport layer (SSL, for instance). Allows compatibility with ASMX Web Services and clients.
Creating the Client

Now add a new project to the current solution so that you can consume the WCF service created. Add a new Windows Forms Application project to the current solution and use its default name, WindowsFormsApplication1.

Populate the default Form1 with the two Button controls shown in Figure 20-24.

Figure 20-24

Add a Service reference to the WindowsFormApplication1 project, and click the Discover button to locate the WCF service in your solution. When the service is found, click OK (see Figure 20-25).

Figure 20-25

To inform clients of your service, you simply need to inform them of this URL: http://localhost:1039/Service1.svc. Because the WCF service is hosted by the ASP.NET Development server, the port number is dynamically chosen. The port number you will see is likely to be different from that shown.

Add another service reference to the WindowsFormApplication1 project. This time, click the Advanced button at the bottom left of the Add Service Reference dialog, and then click the Add Web Reference button at the bottom left of the Service Reference Settings dialog (see Figure 20-26).

Figure 20-26

In the Add Web Reference dialog, click the Web services In the This Solution link and click Service1. Use the default name of localhost, and click the Add Reference button to add a web reference to the project (see Figure 20-27).

Figure 20-27

Double-click the Use wsHttpBinding button in Form1, and code it as follows:

private void btnwsHttpBinding_Click(object sender, EventArgs e) {

 ServiceReference1.Service1Client client =

  new ServiceReference1.Service1Client('WS');

 MessageBox.Show('Using wsHttpBinding: ' +

  client.GetData(5));

 client.Close();

}

Double-click the Use basicHttpBinding button, and code it as follows:

private void btnBasicHttpBinding_Click(object sender, EventArgs e) {

 localhost.Service1 ws = new localhost.Service1();

 MessageBox.Show('Using basicHttpBinding: ' + ws.GetData(6, true));

}

Set the WindowsFormApplication1 project as the startup project, and press F5 to test it. Click both buttons (see Figure 20-28) to access the WCF service using WSHttpBinding and BasicHTTPBinding.

Figure 20-28

This example shows that you can have one WCF service exposed via different endpoints — traditional ASMX Web Service clients can connect to the service using the basicHttpBinding binding, while the rest can connect using the wsHttpBinding binding.

Creating Self- Hosted WCF Service

So far, all the WCF services you have seen are hosted using either a web server or the WCF Service Host. This section shows how you can host a WCF service right from within a Windows Forms application. This example can also be used with the netTCPBinding binding.

The example application is a simple message server that allows clients to send messages to it. Messages received by the service are displayed in a Windows Form.

Creating the WCF Service

Launch Visual Studio 2008 and create a new Windows Forms Application project. Name the project MessageServer.

Populate the default Form1 with a TextBox control, and set its MultiLine property to true (see Figure 20-29).

Figure 20-29 

Add a new item to the project. Select the WCF Service template, and name it MessageService.cs (see Figure 20-30).

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

0

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

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