WCF Chapter II contract to implement multiple contracts and endpoints in a service

A service is defined as a series of endpoints. Each endpoint has an address, binding, and contract. Contract is exposed endpoint capabilities. The address is the application or service can be found which address network contract is about how to access them.
There are many relationship between endpoints and contracts. A stopping point can have only one contract, but a contract can be referenced by many endpoints. Although one endpoint may only confirm a contract, the interface enabling the polymerization to expose a plurality of separate contract interfaces. In addition, there are multiple endpoints different but equally binding contract may be in the same address, give the illusion of a single termination point to implement all contracts.
Exposure to a contract through multiple endpoints in a service, you can make the service can be accessed at different binding. You can define an endpoint using the WS-I basic protocol bindings to maximize traffic while using the other to use the TCP protocol and binary-coded endpoints to achieve faster performance. By multiple interfaces becomes the illusion of an interface, you can access a single service, initially codified into separate interface capabilities by merging.
List 2.10 expose multiple endpoints in a contract in
 
      
using System;
using System.ServiceModel;
using System.Threading;

namespace EssentialWCF
{
[ServiceContract]
public interface IGoodStockService
{
[OperationContract]
double GetStockPrice( string ticker);
}
[ServiceContract]
public interface IGreatStockService
{
[OperationContract]
double GetStockPriceFast( 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 ;
}
}
}
2.11 a list of three contracts expose multiple endpoints profile. There is a termination point IGoodStockService contract, for the two endpoints IGreateStockService contract and termination of a contract for the IAllStockServices.
Because there are a plurality of endpoints share addressing scheme, each endpoint must define different address. Use a relative address, so the full address of each endpoint is a service base address plus the relative address.
2.11 exposed more than a list of endpoints in a service
 
      
<? xml version="1.0" encoding="utf-8" ?>
< configuration >
< system.serviceModel >
< services >
< service name ="EssentialWCF.StockServices"
behaviorConfiguration ="mexServiceBehavior" >
< host >
< baseAddresses >
< add baseAddress ="http://localhost:8000/EssentialWCF" />
</ baseAddresses >
</ host >
< endpoint name ="GoodStockService"
binding
="basicHttpBinding"
contract
="EssentialWCF.IGoodStockService" />
< endpoint name ="BetterStockService"
address
="better"
binding
="basicHttpBinding"
contract
="EssentialWCF.IGreatStockService" />
< endpoint name ="BestStockService"
address
="best"
binding
="wsHttpBinding"
contract
="EssentialWCF.IGreatStockService" />
< endpoint name ="AllStockServices"
address
="all"
binding
="wsHttpBinding"
contract
="EssentialWCF.IAllStockServices" />
< endpoint name ="mex"
binding
="mexHttpBinding"
contract
="IMetadataExchange"
</service
>
</ services >
</ system.serviceModel >
</ configuration >
Because IGreatStockService contract exposed on multiple endpoints, the client application must reference the endpoint name when creating a proxy instance for the contract. If the end point name is not confirmed, WCF will throw an error because it can not know which endpoint to use. 2.12 shows a list of GreatStockServiceClient agent used twice: once using basicHttpBinding access BetterStockService, once with wsHttpBinding access BestStockService.
List 2.12 when multiple endpoints are defined using the name to confirm each endpoint
 
      
using (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 " ));
}

==========

forward from

Author: DanielWise
Source: http://www.cnblogs.com/danielWise/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page otherwise, the right to pursue legal responsibilities.
 

Reproduced in: https: //www.cnblogs.com/llbofchina/archive/2011/06/27/2091372.html

Guess you like

Origin blog.csdn.net/weixin_33816946/article/details/94206772