Friday 31 August 2012

WCF - Notes 4 WCF Client


Creating a WCF Client

WCF Client talks to WCF service with a proxy.


A WCF client can use a proxy to access a service.
  • Creating a proxy requires knowing what contract is exposed by the target endpoint, and then using the contract’s definition to generate the proxy. In WCF, this process can be performed using either Visual Studio or the command-line svcutil tool.
  • If the service is implemented using WCF, these tools can access the service’s DLL to learn about the contract and generate a proxy. If only the service’s WSDL definition is available, the tools can read this to produce a proxy.
  •  If only the service itself is available, Visual Studio and svcutil can access it directly using either WS-MetadataExchange or a simple HTTP GET to acquire the service’s WSDL interface definition, and then generate the proxy.
  • However it’s generated, the client can create a new instance of the proxy, and then invoke the service’s methods using it.
  • Good programming practice to clean up the communications infrastructure that has been created.
  • One more thing remains to be specified by the client: the exact endpoint on which it wishes to invoke operations. Like a service, the client must specify the endpoint’s contract, its binding and its address, and this is typically done in a config file. In fact, if enough information is available, svcutil will automatically generate an appropriate client configuration file for the target service.
WCF supports several possibilities along with traditional RPC, including the following:
·         Asynchronous RPC, with non-blocking paired calls carrying lists of typed parameters.
·         Traditional messaging, with non-blocking calls carrying a single message parameter. Working directly with channels exposes methods to send and receive messages, and WCF defines a standard Message class that can be used to work directly with XML messages.
·         Direct manipulation of SOAP messages using the WCF-defined attribute MessageContract. Using two related attributes, MessageHeader and MessageBodyMember, an application can explicitly access the contents of a SOAP message’s header and body.
  • To allow creating a method that sends information but doesn’t block waiting for a response, the OperationContract attribute has a property called IsOneWay. Methods marked with this attribute and property can have only input parameters and must return void.
[OperationContract(IsOneWay=true)]
void NewCarAvailable(int vehicleClass, int location);
  • With this method, the caller gets nothing in return—it’s one-way communication. A typical use of one-way methods like this is to send unsolicited events.
  • It’s also possible to link together calls to methods, whether one-way or normal two-way methods, allowing both sides of the communication to act as client and service. To do this, each side implements a contract that’s linked with its partner, resulting in what are called duplex contracts. WCF provides special bindings for handling this case, such as WsDualHttpBinding for duplex contracts that use standard interoperable SOAP.

Controlling Local Behavior

  • How is concurrent access to a service instance managed, for example, and how is that instance’s lifetime controlled? To allow developers to set local behaviors like these, WCF defines two primary attributes, both of which have a number of properties. One of these attributes, ServiceBehavior, can be applied to any service class. The other, OperationBehavior, can be applied to any method in a service class that is also marked with the OperationContract attribute.
  • The ServiceBehavior attribute has various properties that affect the behavior of the service as a whole. For example, a property called ConcurrencyMode can be used to control concurrent access to the service. If set to Single, WCF will deliver only one client request at a time to this service, i.e., the service will be single-threaded. If this property is set to Multiple, WCF will deliver more than one client request at a time to the service, each running on a different thread. Similarly, ServiceBehavior’s InstanceContextMode property can be used to control how instances of a service are created and destroyed. If InstanceMode is set to PerCall, a new instance of the service will be created to handle each client request, and then destroyed when the request is completed. If it’s set to PerSession, however, the same instance of the service will be used to handle all requests from a particular client. (Doing this also requires setting ServiceContract‘s Session attribute to true and choosing a binding that supports sessions.) InstanceContextMode can also be set to Single, which causes a single instance of the service to handle all requests from all clients.
using System.ServiceModel;
[ServiceContract]
[ServiceBehavior(
   ConcurrencyMode=Multiple,
   InstanceContextMode=Single)]
class RentalReservations { ... }
  • Similarly, properties on the OperationBehavior attribute allow controlling the impersonation behavior of the method that implements a particular operation, its transactional requirements (described later), and other things.
  • Another aspect of local behavior is how a request from a client is routed within a WCF service. WCF in the .NET Framework 4 adds a standard content-based routing service that a developer can use with SOAP-based services. The routing service sits in front of other WCF services, routing each incoming message to one of those services based on the message’s content. 

No comments:

Post a Comment