|
Get Acquainted With SOA and Indigo (Continued)
Services written with WCF are scale-invariant. Services can scale up, out, down, and in. Services can scale up through deployment on more powerful servers, where WCF can leverage more resources. Services can scale out through routing and server farms, distributing traffic to multiple instances of services. Services can scale down by interacting with service-oriented devices such as printers and cameras. Services can scale in using efficient cross-process transports. Scale invariance maximizes the deployment options for your application.
Services are described at the contract layer. The service runtime layer loads and runs services, managing many behaviors such as instancing and concurrency. The messaging layer provides the communication infrastructure, which includes various communication channels for transport, reliable messaging, security, and queuing. The activation and hosting layer allows services to run in a variety of environments, ranging from a self-hosted EXE to automatic activation and hosting by Microsoft Internet Information Services (IIS).
Code in WCF
WCF simplifies service programming from a Visual Studio standpoint. You can take a "code-first" or "contract-first" approach to development. Tools assist development by generating code from service descriptions and vice versa.
The programming model allows you to perform common tasks with only a little code, yet it gives you full control when you want it. There are hundreds of classes in the complete object model, but you'll primarily work with a few high-level objects such as services, channels, and messages. WCF code is compact, easy to write, and easy to understand. For example, note how little code it takes to initiate communication with another service:
TradingSystemProxy proxy = new TradingSystemProxy();
price = proxy.GetQuote(symbol);
The programming model offers three methods for writing programs: declarative programming using attributes, imperative programming using code and objects, and configuration-based programming through application configuration file settings. You typically declare contracts and service behaviors using attributes; configure end points, security, and management features using config files; and implement service operations in code.
Several levels of programming are possible. Typed services contain service operations that are similar to functions, with parameters and return values that might be simple or complex data types. Untyped services contain service operations that accept and return messages. You work directly with messages at this level. Programming against the messaging layer is the lowest level of all, giving you precise control over communication activities such as creating channels.
The programming model includes a collection of declarative attributes, known as the service model, that you can use in place of explicit code. The service model gives you attribute-based control over many areas, including communication, security, transactions, instancing, and error handling. To get a sense of this, take a look at this code fragment where a service contract specifies details about session, communication, and transaction behavior through the use of attributes:
[ServiceContract(Session=true)]
[ServiceBehavior(InstanceMode=
InstanceMode.PrivateSession)]
public class MyService
{
[OperationContract(IsOneWay=false)]
[OperationBehavior(AutoEnlistTransaction=true)]
String DebitAccount(string accountNo,
double amount)
{
...
}
}
You can also specify many service model capabilities as configuration settings, allowing deployment-time decisions to be made about communication, security, and runtime behavior. The service model allows you to focus on what's important: your application. In a WCF program, most of the code will be your application code.
WCF's communication can be either synchronous or asynchronous, and one-way or two-way. It can also use datagram, duplex, or request-reply messaging patterns. The transport for communication can be HTTP, TCP, or named pipes (for cross-process communication). It's possible to add new transports by writing a transport provider.
A message can contain any digital information, from a simple string to a business data structure to video content. WCF represents messages in memory as SOAP envelopes, and you can render these in text, binary, or Message Transmission Optimization Mechanism (MTOM) format for transmission. Messages can be buffered (transferred in entirety) or streamed.
As a developer, you can specify your communication, reliability, and security requirements (called bindings); WCF builds the appropriate types of communication channels in response. More than a dozen predefined "bindings" delineate the combinations needed for common scenarios, but you remain free to create your own custom bindings. You can also specify bindings in code or configuration files.
Back to top
|