Friday, August 10, 2007

The InstanceContextMode Property

You can apply the ServiceBehavior attribute with the InstanceContextMode property set to the following:
  • InstanceContextMode.PerCall - Per-Call Services.
  • InstanceContextMode.PerSession - Per-Session Services, default.
  • InstanceContextMode.Sharable - Shareable Services, not included in the current release.
  • InstanceContextMode.Single - Singleton Services, with sessions or without sessions.

A service that implements a session-aware contract requires that all the endpoints that expose the contract use bindings that support reliable transport session. One exception to this rule is the named pipes binding.

A shareable service behaves much like a per-session service, with one important additional aspect: the instance has a unique ID, and when a client establishes a session with a shareable service, the client can pass a logical reference to that instance to another client. The second client will establish an independent session but will share the same instance. Also, each of these sessions may use different inactivity timeouts, and expire independently of any other session.

WCF Essentials: Discover Mighty Instance Management Techniques For Developing WCF Apps by Juval Lowy.

Tuesday, August 07, 2007

Some Basic WCF Concepts

WCF unifies the existing suite of .NET distributed technologies into a single programming model that improves the overall developer experience through a consistent architecture, new levels of functionality and interoperability, and all the extensibility points you could want. WCF was designed according to the tenets of service orientation.

A service is a piece of code you interact with through messages. Services are passive. They wait for incoming messages before doing any work. Clients are the initiators. Clients send messages to services to request work.

Services expose one or more endpoints where messages can be sent. Each endpoint consists of an address, a binding, and a contract. The address specifies where to send messages. The binding describes how to send messages. And the contract describes what the messages contain. Clients need to know this information before they can access a service.

Services can package up endpoint descriptions to share with clients, typically by using Web Services Description Language (WSDL). Then clients can use the provided service description to generate code within their environment capable of sending and receiving the proper messages.

WCF Programming Model

With WCF, you're either writing services that expose endpoints or you're writing clients that interact with endpoints. Hence, endpoints are central to the WCF programming model and infrastructure.

When building a WCF service, you typically start by defining a .NET interface definition to serve as the service contract. Then you implement the service contract in a .NET class, known as the service type, and configure its behavior. Next, you define the endpoints the service will expose, specifying the address, binding, and contract for each one. Finally, you host the service type in an application using the WCF hosting infrastructure. Once the service type is hosted, clients can retrieve its endpoint descriptions and begin integrating with it.

When building a WCF client, you first need the description of the target endpoint you want to access. The endpoint description can be used to dynamically create a typed proxy. Then you can write code against the typed proxy to access the service by sending the appropriate messages to the target endpoint.

Service Contracts and Dispatch Behavior

WCF uses the information found in the service contract to perform dispatching and serialization. Dispatching is the process of deciding which method to call for an incoming SOAP message. Serialization is the process of mapping between the data found in a SOAP message and the corresponding .NET objects used in the method invocation. This mapping is controlled by an operation's data contract.

Data Contracts

The way WCF serializes .NET classes depends on the serialization engine in use. The default serialization engine is known as DataContract, a simplified version of XmlSerializer, the default serialization engine used in ASMX today.

With DataContract, only fields marked with DataMember will be serialized. And you can serialize private fields.

Attribute List

ServiceContract, OperationContract, DataContract, DataMember, MessageContract, MessageBody, MessageHeader, ServiceBehavior.

From Distributed .NET: Learn The ABCs Of Programming Windows Communication Foundation by Aaron Skonnard.