Saturday, October 16, 2010

Transactions in WCF: (WCF Part 11)


The most important part to understand from my opinion when working with WCF is the transaction part.
A transaction treats a group of operations as an atomic unit, so either all succeed or all fail.

WCF supports two types of transactions: shared transactions and transacted messaging.

WCF supports two transaction protocols:

  1. OleTransaction protocol: Used for transaction control between WCF applications.
  2. WS-AtomicTransaction protocol: Enables WCF applications to flow transactions to interoperable applications, such as Web services that have been built by using third-party technology.

Transaction Process (Client & Server Side)

1-Client Side
Clients use a TransactionScope object to group operations into transactions:



using (TransactionScope sc = new TransactionScope())







As well as specifying transaction requirements, the client can control the isolation level and timeout for the transaction by using a TransactionOptions object:



TransactionOptions top = new TransactionOptions();

    top.IsolationLevel = IsolationLevel.ReadCommitted;

    using (TransactionScope sc = new TransactionScope(TransactionScopeOption.RequiresNew, top)) //...


2-Server Side

The ServiceBehavior attribute enables you to configure the service as a whole for transaction time-outs, isolation level, and whether transactions complete when a session closes.
The OperationBehavior attribute helps you to control whether a transaction is required for an operation, and whether transactions complete if no exception is thrown.

WCF can use Microsoft Message Queuing, or MSMQ, to pass messages between clients and services. Queuing can improve application resilience and scalability.

Reliable Sessions in WCF:

A reliable session provides session-based delivery for WCF messages, regardless of the number or type of transport intermediaries between two endpoints.

Reliable sessions handle lost or duplicated messages. Messages are delivered exactly once, and can optionally be delivered in order. If a message cannot be delivered, the sender is informed.

They are enabled by default for theWsDualHttp, NetNamedPipeandMsmqIntegrationbindings, and can be enabled for theNetTcp, WsHttpandWsHttpFederationbindings.


Finally, I’d like to thank all of you for your Attention and hope that you’ve learned something new from these WCF 11 parts that I’ve written :)

Mohamed Adel Mahmoud.
Microsoft Intern – Senior Microsoft Student Partner – Microsoft Egypt.
  For More Info Please Visit: WCF Resources Page  

Security Features of WCF: (WCF Part 10)


This Time I’ll be focusing more on the Security Part in WCF.

WCF provides comprehensive security features in three main areas: transfer security, authentication and authorization of clients, and auditing.


Security Modes of WCF:

WCF can provide authentication, privacy, and integrity for messages by using two mechanisms:

  1. Transport mode: which uses the security features of a transport layer such as HTTPS. This mode has performance benefits due to the optimized nature of the underlying protocols, but it has a restricted set of credential or claim types. This mode also only works between two transport endpoints.
  2. Message mode: which protects the message itself by using a protocol such as WS-Security.

You can specify the security for a binding by setting its SecurityMode property. By default, the BasicHttpBinding has no security configured. Other HTTP bindings use WS-Security, and TCP and Named Pipe bindings use Windows security.

You can use the .NET PrincipalPermission attribute to restrict access to an operation based on name, role, or authentication status.


  For More Info Please Visit: WCF Resources Page  

Guidelines for Configuring the Transport Layer: (WCF Part 9)


NATs and Firewalls:
Firewalls and Network Address Translators (NATs) may make it impossible to use duplex contracts, since NATs hide IP addresses. Firewalls commonly restrict the protocols and ports that can be used, and this may restrict services to using HTTP or HTTPS as a transport.

Streaming Message Transfer:
By default, entire messages are buffered in memory by WCF transports. However, you can eliminate the need for large memory buffers by exposing the message body as a stream.

Unidirectional or bidirectional streaming is enabled through the TransferMode property of the transport binding element.

Note that there are some restrictions on the use of streaming, especially when using certain WCF features, such as reliable messaging, transactions, and SOAP security, which may require buffering.

Transport Quotas:
Transport quotas should be used to ensure that connections do not consume excessive resources.

WCF supports two main types of quota: timeouts to guard against denial of service attacks and allocation limits to guard against excessive memory use.

  For More Info Please Visit: WCF Resources Page  

Binding in WCF: (WCF Part 8)


Choosing a Predefined Binding in WCF:
WCF has nine built-in bindings.e.g.(
NetTcpBinding and other Built-in Bindings )

Defining a Custom Binding in WCF:

A binding is made up of a collection of binding elements, each of which describes some feature of the endpoint's communication. It is important to know about transports, encoding, and other elements when you create a custom binding, because you must choose which elements to put into the stack. A binding must specify at least a transport and an encoder. For example, the NetTcpBinding combines the TCP transport with a binary encoder.

You may wish to create a new binding to accommodate a new transport or encoding.

WCF provides three predefined transports:

  1. HTTP: Used for Web service communication.
    You would choose HTTP if you want to do one of the following:
    1. Host services in IIS 6.0 or later.
    2. Communicate across machines.
    3. Provide good tool support for development, diagnosis, and other activities.
  2. TCP: Typically used for binary communication across computers.
    You would choose TCP if you want to do one of the following:
    1. Provide minimal latency and maximal throughput.
    2. Communicate across computers.
  3. Named Pipes: Provide efficient communication between applications on the same computer.
    You would choose Named Pipes if you want the most efficient communication on a single computer.

In addition to a transport, a binding requires a message encoder to serialize a WCF message into bytes.

WCF has three encoders to handle text, binary, and MTOM data. The text encoder supports plain old XML (POX) as well as SOAP encoding. If your encoding requirement is not handled by these three encoders, you can write your own custom encoder.

  For More Info Please Visit: WCF Resources Page  

Asynchronous Invocation in WCF (WCF Part 7)


The client calls the Begin operation by passing any required parameters, a callback function, and a state object. The callback function is called when the operation finishes.

object can be any object you want to pass to the callback function and in WCF it is usually a reference to the proxy:


IAsyncResult iar = proxy.BeginAdd(i1, i2, MyCallback, proxy);

/*This function returns immediately and provides an IAsyncResult that the caller can use to check the state of the operation.*/



The callback function is executed when the service operation completes, and the callback function calls the matching End method:



static void MyCallback(IAsyncResult iar)


   double val = ((AddProxy)iar.AsyncState).EndAdd(iar);




WCF Service Behavior:

it's very necessary to demonstrate how the clients will connect with the WCF Services (e.g. How many service instances will be used to serve the clients, operation counts, Error handling, Security, such as impersonation, authorization, Metadata   etc)

Behaviors may be code-only, or may also use configuration settings. e.g.



/*This line of code will make all the clients using the same instance of the service.*/


OR you can use Throttling:

Throttling allows you to set limits on access to a service, such as the number of connections or pending calls.

You configure throttling in the configuration file:


<throttling maxConcurrentCalls = "12" maxConnections = "34" maxInstances = "56" />


Developers can also retrieve the throttling settings at runtime, and modify them before opening the service.

Developers can create custom behaviors by using IServiceBehavior and IEndpointBehavior.
  For More Info Please Visit: WCF Resources Page  

Search This Blog