A service is defined as a series of endpoints. Each endpoint has an address, binding, and contract. A contract is the ability to expose an end point. The address is where these apps or services are located from the network address, and the contract is about how to access them. There is a one-to-many relationship between the endpoint and the contract. An endpoint can have only one contract, but a contract can be referenced by many endpoints. Although an endpoint can simply confirm a contract, interface aggregation enables a single contract to expose multiple interfaces. In addition, multiple endpoints with the same binding but different contracts can be at the same address, giving the illusion of all contracts to a single endpoint. By exposing a contract to multiple endpoints in a service, you can make the service accessible in different bindings. You can define an endpoint that uses the WS-I Foundation protocol bindings to get maximum traffic while using another endpoint that uses the TCP protocol and binary encoding to achieve faster performance. By turning multiple interfaces into the illusion of an interface, you can initially compile into a separate interface capability by merging the access in a single service. Listing 2.10 Exposing multiple contracts in one endpoint
using system;using system.servicemodel;using System.Threading;namespace essentialwcf{[ServiceContract] public interface Igoodstockservice {[OperationContract] double Get StockPrice (string ticker); } [ServiceContract] public interface Igreatstockservice {[OperationContract] double GETSTOCKPRICEF AST (string ticker); } [ServiceContract] public interface Iallstockservices:igoodstockservice, igreatstockservice{}; public class Allstockservices:iallstockservices {public double Getstockprice (string ticker) { Thread.Sleep (5000); return 94.85; } public double Getstockpricefast (string ticker) {return 94.85; } }}
List 2.11 A configuration file that exposes multiple endpoints for three contracts. There is an endpoint for the Igoodstockservice contract, two endpoints for the Igreatestockservice contract, and one for the iallstockservices contract. Because there are multiple endpoints that share an addressing scheme, each endpoint must define a different address. Relative addresses are used, so the full address of each endpoint is the service base plus the relative address. Listing 2.11 Exposing multiple endpoints in one service
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <system.serviceModel> <services> &L T;service name = "Essentialwcf.stockservices" behaviorconfiguration= "Mexservicebehavior" > Because the Igreatstockservice contract is exposed on multiple endpoints, the client application must reference the endpoint name when creating a proxy instance for the contract. If the endpoint name is not confirmed, WCF throws an error because it cannot know which endpoint to use. Listing 2.12 shows two uses for the greatstockserviceclient agent: one time using BasicHttpBinding access Betterstockservice, and another with wshttpbinding access Beststockservice. Listing 2.12 When multiple endpoints are defined use the name to confirm each endpointusing (localhost. Greatstockserviceclient proxy = new Client.localhost.GreatStockServiceClient ("Betterstockservice")) { Console.WriteLine (proxy. Getstockpricefast ("MSFT"));} using (localhost. Greatstockserviceclient proxy = new Client.localhost.GreatStockServiceClient ("Beststockservice")) { Console.WriteLine (proxy. Getstockpricefast ("MSFT"));}
Implementing multiple contracts and endpoints in a service Z