Книга: C# 2008 Programmer

Exposing Multiple Endpoints

Exposing Multiple Endpoints

A WCF service can expose multiple endpoints. Follow along to build a WCF service that exposes endpoints using two different bindings: WSHttpBinding and BasicHttpBinding.

Creating the WCF Service

Using Visual Studio 2008, create a new WCF Service Application and name it MultipleEndpointsService (see Figure 20-20).


Figure 20-20

In this example, the WCF service is hosted by the ASP.NET Development Server, a web server shipped with Visual Studio 2008. Because the service is hosted by a web server, the NetTcpBinding binding is not supported.

Edit the Web.config file by right-clicking it in Solution Explorer and selecting Edit WCF Configuration. (You can also launch the WCF Service Configuration Editor by selecting Tools?WCF Service Configuration Editor.)

Expand the Endpoints node, and select the first endpoint. Name it WS (see Figure 20-21).


Figure 20-21

Right-click on the Endpoints node, and select New Service Endpoint to add a new endpoint to the service (see Figure 20-22).


Figure 20-22

Name the new endpoint BASIC, and set its various properties as indicated (see Figure 20-23).


Figure 20-23 

Property Value
Address asmx
Binding basicHttpBinding
Contract MultipleEndpointsService.IService1

Save and close the Web.config file. Build the MultipleEndpointsService 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.

Оглавление книги


Генерация: 1.163. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз