Saturday, January 17, 2009

WCF Fundamentals - One should Know!

WCF Fundamentals

· Before switch to Programming gear in the upcoming blogs, let’s have a quick look on some fundamental concepts.

· WCF programs communicate through the exchange of messages.

- A message is a self-contained unit of data consists of header and a body.

- A message should contain one body and any number of headers.

- All Indigo messages are represented as XML, specifically SOAP envelopes containing XML
Infosets.

· There are three types of Messaging programs: Clients, Services and Intermediaries.

- Client program initiate and send the request message to Service program.

- Service program respond to the client message. The input message may cause the service to perform some action like code execution or reply back to the client with an output message.

- Intermediary is a program between Service and Client which do some tasks like routing, acting as gateway, monitoring etc…

ABC’s of WCF

· Out of the box, WCF provides the following encodings:

- Text encoding, an interoperable encoding.

- Message Transmission Optimization Mechanism (MTOM) encoding, this is an interoperable way for efficiently sending unstructured binary data to and from a service.

- Binary encoding for efficient transfer.

· One can have more encoding mechanisms like a compression encoding using the built-in extension points of WCF.

· WCF supports the following messaging patterns

- Simplex – One Way Messaging (Fire and Forget).
- Duplex – Asynchronous two-way messaging. (Ex: Remotely controlled robot).
- Request: Reply – Synchronous two-way messaging. (Ex: XML Web services)


Indigo Service – Internally

· Internally, Indigo service contains contracts, bindings, endpoints and implementation code.

· Address: Where is the service?

· An address defines where messages can be sent. The format of an endpoint address depends on the transport mechanism.

- For instance, for HTTP Transport it looks like http://www.objectinnovations.com:8080/nf3

- For TCP, it looks likes net.tcp://www.objectinnovations.com:9000/nf3

- The endpoint address enables you to create unique endpoint addresses for each endpoint in
a service.

· Binding: How do I communicate to the service?

· Binding provides information on how a service can be accessed including transport method (HTTP / HTTPS / TCP / Named pipe / MSMQ), encoding format (Text / Binary /MTOM), security mechanism, reliability requirement etc...

- A service to be accessible, at least it should be associate with either one or more bindings.

- A contract can support many bindings and a binding can support many contracts.

· Out of the box, Indigo support following bindings which covers most of the common scenarios.

- Developers can create their own custom binding based on their requirement with the mix of functionality.

· Contract: What can the service do for me?

- A contract explains a service’s behavior, structures, or message formats.

- There are three types of contracts in Indigo Services including Service contracts, Data contracts and Message contracts.

· You define service contracts, data contracts, and message contracts by using declarative attributes.

· The class that needs to be exposed as a WCF service should be marked with ServiceContract attribute.

· Service contracts can be defined by annotating an interface with [ServiceContract].

· Identify service operations by annotating methods with [OperationContract].

· A service contract defines service-level settings, such as the namespace of the service, a corresponding callback contract, etc.

- In comparison to object oriented programming, the service contract is nothing but an interface.

- All methods that need to be invoke using SOAP message should be marked with OperationContract attribute.

- An operation contract defines the parameters and return type of an operation.

· A WCF service can have multiple service contracts.

· For instance, a service contract looks as follows:
[ServiceContract]

public interface IBook
{
[OperationContract]
bool BookAvailable(string bookName, string authorName, int bookID)
[OperationContract]
bool AddCart(Book book)
[OperationContract]
void OrderBook(int bookID)
}

· By default service contracts handle simple service calls. Data contract defines custom data structures like object or struct.

· If the service is using only simple types, there is no need to explicitly use data contracts. In Data Contracts, the data can be passed to and from services in XML Schema form.

· For instance, a data contract looks as follows:
[DataContract]
public class Book
{
[DataMember]
public string bookName;
[DataMember]
public string authorName;
………
}
· Contrast to ASMX Web services, in WCF you need to decorate a class with Data contract attributes, to allow serialization.

· Data Contract specifies how the data is serialized and de-serialized.

· A message contact describes the format of a message. For example, it declares whether message elements should go in headers versus the body, what level of security should be applied to what elements of the message, and so on.

· Externally, Indigo service exposes a service description (WSDL, WS-Policy) and one or more endpoints, with each endpoint exposing one or more service operations.

· An endpoint connects a contract and a binding with an address. At least one endpoint needs to be there to access that service. A service can have multiple endpoints.

· For instance,
- Endpoint1 = Address1 (http://www.objectinnovations.com/WCF/) + Binding1 (Http / X.509 Cert / MTOM Encoding) + Contract1 (IContract1 interface)
- Endpoint 2 = Address 2 (net.tcp://www.objectinnovations.com/WPF/) + Binding2 (TCP / Windows Security / Binary Encoding) + Contract2 (IContract2 interface)
- Endpoint 3 = Address3 (net.msmq://www.objectinnovations.com/WF/) + Binding3 (MSMSQ / Binary Encoding) + Contract2 (IContract2 interface)
- A service can have many endpoints (contract bound to address) coexisting and available at the same time.

Indigo Service – Externally

· If you want to expose your service via HTTP and use SOAP 1.1 for maximum interoperability, and also want to expose it via TCP using a binary wire encoding for maximum performance, the two resulting endpoints can reside side-by-side on top of the very same service!

- Endpoints: Addresses, Bindings, and Contracts!

· An endpoint consists of four properties:

- An address that specifies where the endpoint can be found.

- A binding that specifies how a client can communicate with the endpoint.

- The contract specifies what functionality the endpoint exposes to the client.

- A set of behaviors that specify local implementation details of the endpoint.

No comments: