wcf by sandyk2012


									Introduction of WCF
Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows.and it
(Code named Indigo) is a programming platform and runtime system for building, configuring and deploying
network-distributed services.WCF provides a runtime environment for your services, enabling you to expose CLR
types as services, and to consume other services as CLR types. It is the latest service oriented technology;
Interoperability is the fundamental characteristics of WCF. Windows Communication Foundation (WCF) is unified
programming model provided in .Net Framework 3.0. WCF is a combined features of Web Service, Remoting, MSMQ
and COM+. WCF provides a common platform for all .NET communication.


      Its made of a lot of different components, so you can create new components for security, transport,
      In WCF, there is no need to make much change in code for implementing the security model and changing
       the binding. Small changes in the configuration will make your requirements.
      Its faster than ASMX
      Supports Scaling/Load Balancing
      WCF has integrated logging mechanism, changing the configuration file settings will provide this
       functionality. In other technology developer has to write the code.
      Its Interoperability, for java, and more.
      WCF is interoperable with other services when compared to .Net Remoting,where the client and service
       have to be .Net
      WCF services provide better reliability and security in compared to ASMX web services.


A drawback would be that, WCF is Microsoft's implementation of SOA and hence its APIs are solely controlled by MS
which makes interoperability a bit difficult. Also, to deploy WCF apps, you will need more underlying hardware
resources on the platform on which the WCF applications will be running, since there is an additional layer of
abstraction to deal with.

WCF Architecture
There are five layers:-
3)Service Runtime
5)Activation and hosting


Contracts layer are second layer of the wcf architecture. Developer will directly use this contract to develop the
service. We are also going to do the same now.

i) Service contract

-It explains about the operation that service can provide.

ii) Data contract

- It explains the custom data type which is exposed to the client and defines the data types, are passed to and from
service. Data types like int, string are identified by the client because it is already mention in XML schema definition
language document, but custom created class or datatype cannot be identified by the client e.g. Employee data type.
By using DataContract we can make client aware that we are using Employee data type for returning or passing
parameter to the method.

iii) Message Contract

- Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it
is not meeting your requirements then we can create our own message format. This can be achieved by using
Message Contract attribute.

iv) Policies and Binding

- It specify conditions required to communicate with a service e.g security requirement to communicate with
service, protocol and encoding used for binding.

Service Runtime

- It contains the behaviors that occur during runtime of service.

       Error Behavior - Specifies what occurs, when internal error occurs on the service.
       Dispatch Behavior - Controls how a message is processed by the WCF Infrastructure.
       Throttling Behavior- Controls how many messages are processed.
       Metadata Behavior - Tells how and whether metadata is available to outside world.
       Instance Behavior - Specifies how many instance of the service has to be created while running.
       Transaction Behavior - Enables the rollback of transacted operations if a failure occurs.


It is composed of channels and channel is a component that processes a message in some way, for example, by
authenticating a message. Channels are the core abstraction for sending message to and receiving message from an
Endpoin and set of channels is also known as a channel stack.t. Broadly we can categories channels as

       Transport Channels: Handles sending and receiving message from network. Protocols like HTTP, TCP, name
        pipes and MSMQ.
       Protocol Channels: Implements SOAP based protocol by processing and possibly modifying message. E.g.
        WS-Security and WS-Reliability.

Activation and Hosting

Services can be hosted or executed, so that it will be available to everyone accessing from the client. WCF serv ice
can be hosted by following mechanism

       IIS

        Internet information Service provides number of advantages if a Service uses Http as protocol. It does not
        require Host code to activate the service, it automatically activates service code.

       Windows Activation Service

        (WAS) is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based
        communication, WCF can also use WAS to provide message-based activation over other protocols, such as
        TCP and named pipes.

       Self-Hosting

        WCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.

       Windows Service

        WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager

WCF Contracts
In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the
service does. WCF defines four types of contracts.

There are four Contracts:

       Service contracts: Define which operations the client can perform on the service. Service contracts are the
        subject of the next chapter, but are used extensively in every chapter in this book.
       Data contracts: Define which data types are passed to and from the service. WCF defines implicit contracts
        for built-in types such as int and string, but you can easily define explicit opt-in data contracts for custom
       Fault contracts: Define which errors are raised by the service, and how the service handles and propagates
        errors to its clients.
       Message contracts: Allow the service to interact directly with messages. Message contracts can be typed or
        untyped, and are useful in interoperability cases and when there is an existing message format you have to
        comply with.

WCF Service
A service-oriented application (SOA) aggregates services into a single logical application similar to the way a
component-oriented application aggregates components or an object-oriented application aggregates objects and
ervice-orientation (SO) is an abstract set of principles and best practices for building SO applications .The services
can be local or remote, developed by multiple parties using any technology, versioned independently, and even
execute on different timelines. Inside a service, you will find concepts such as languages, technologies, platforms,
versions, and frameworks, yet between services, only prescribed communication patterns are allowed.The client of a
service is merely the party consuming its functionality. The client can be literally anythinga Windows Forms class, an
ASP.NET page, or another service

Clients and services interact by sending and receiving messages. Messages may transfer directly from client to
service or via an intermediary. With WCF, all messages are SOAP messages.the messages are independent of
transport protocolsunlike Web services, WCF services may communicate over a variety of transports, not just HTTP.
WCF clients may interoperate with non-WCF services, and WCF services can interact with non-WCF clients

Service contracts

interface IMyContract
  string show(string text);

 //Will not be part of the contract
 string show(string text);
class MyService : IMyContract
  public string show(string text)
    return "Hello " + text;
  public string show(string text)
    return "Cannot call this method over WCF";

WCF Addresses
WCF address provides two important elements:

1) The location of the service :-The location portion of the address indicates the name of the target machine, site, or
network; a communication port, pipe, or queue; and an optional specific path or URI. A URI is a Universal Resource
Identifier, and can be any unique string, such as the service name or a GUID.

2) The transport protocol or transport schema used to communicate with the service.

WCF transport schemas

WCF 1.0 supports the following transport schemas
3)Peer network
4)IPC (Inter-Process Communication over named pipes)
Addresses always have the following format:
[base address]/[optional URI]

The base address is always in this format:

[transport]://[machine or domain][:optional port]

The way to read an address such as

http://localhost:8001 is like this: "Using HTTP, go to the machine called localhost, where on port 8001 someone is
waiting for my calls."

If there is also a URI such as:


then the address would read as follows: "Using HTTP, go to the machine called localhost, where on port 8001
someone called

MyService is waiting for my calls."

1) HTTP Addresses:

HTTP addresses use http for transport, and can also use https for secure transport. You typically use HTTP addresses
with outward-facing
Internet-based services, and can specify a port such as:
When the port number is unspecified, it defaults to 80. Similar to TCP addresses, two HTTP addresses from the same
host can share a port, even on the same machine.


TCP addresses use net.tcp for the transport, and typically include a port number such as:

When a port number is not specified, the TCP address defaults to port 808:
It is possible for two TCP addresses (from the same host, which will be discussed more later on in this chapter) to
share a port:

3) IPC Addresses (Inter-Process Communication over named pipes)

IPC addresses use net.pipe for transport, to indicate the use of the Windows named pipe mechanism. In WCF,
services that use named pipes can only accept calls from the same machine. Consequently, you must specify either
the explicit local machine name or localhost for the machine name, followed by a unique string for the pipe name:

4) MSMQ Addresses

MSMQ addresses use net.msmq for transport, to indicate the use of the Microsoft Message Queue (MSMQ). You
must specify the queue name. When you're dealing with private queues, you must specify the queue type, but that
can be omitted for public queues:


5) Peer Network Address

Peer network addresses use net.p2p for transport, to indicate the use of the Windows peer network transport. You
must specify the peer network name as well as a unique path and port. Using and configuring peer networks is
beyond the scope of this book, and you will see very little mention of peer networks in subsequent chapters.

WCF Bindings
A binding explains the transport (such as HTTP or TCP) and the encoding being used (such as text or binary). A
binding can contain binding elements that specify details like the security mechanisms used to secure messages, or
the message pattern used by an endpoint.

WCF defines nine standard bindings:

Basic binding: Defined by the BasicHttpBinding class, this is designed to expose a WCF service as a legacy ASMX web
service, so that old clients can work with new services. When used by the client, this binding enables new WCF
clients to work with old ASMX services.

TCP binding: Defined by the NetTcpBinding class, this uses TCP for cross-machine communication on the intranet. It
supports a variety of features, including reliability, transactions, and security, and is optimized for WCF-to-WCF
communication. As a result, it requires both the client and the service to use WCF.

Peer network binding: Defined by the NetPeerTcpBinding class, this uses peer networking as a transport. The peer
network-enabled client and services all subscribe to the same grid and broadcast messages to it. Peer networking is
beyond the scope of this book since it requires an understanding of grid topology and mesh computing strategies.

IPC binding: Defined by the NetNamedPipeBinding class, this uses named pipes as a transport for same-machine
communication. It is the most secure binding since it cannot accept calls from outside the machine and it supports a
variety of features similar to the TCP binding.

Web Service (WS) binding: Defined by the WSHttpBinding class, this uses HTTP or HTTPS for transport, and is
designed to offer a variety of features such as reliability, transactions, and security over the Internet.

Federated WS binding: Defined by the WSFederationHttpBinding class, this is a specialization of the WS binding,
offering support for federated security.

Duplex WS binding: Defined by the WSDualHttpBinding class, this is similar to the WS binding except it also supports
bidirectional communication from the service to the client.

MSMQ binding: Defined by the NetMsmqBinding class, this uses MSMQ for transport and is designed to offer
support for disconnected queued calls. MSMQ integration binding
defined by the MsmqIntegrationBinding class, this converts WCF messages to and from MSMQ messages, and is
designed to interoperate with legacy MSMQ clients.

WCF Hosting

WCF service has to be hosted in windows process called as host process. Single host process can host multiple
servers and same service type can be hosted in multiple host process

There are mainly four different way of hosting the WCF service.

1)IIS hosting
2)Self hosting
3)Windows Activation Service
4)Windows Service

The WCF host architecture

The WCF context is conceptually similar to the Enterprise Services context or the .NET context-bound object context
Multiple hosting and protocols supported by WCF. Microsoft has introduced the WCF concept in order to make
distributed application development and deployment simple.

                        Hosting Environment                                     Supported protocol
Windows console and form application                                    HTTP,net.tcp,net.pipe,net.msmq
Windows service application (formerly known as NT services)             HTTP,net.tcp,net.pipe,net.msmq
Web server IIS6                                                         http, wshttp
Web server IIS7 - Windows Process Activation Service (WAS)              HTTP,net.tcp,net.pipe,net.msmq

WCF Endpoints

In WCF the relationship between Address, Contract and Binding is called Endpoint. The Endpoint is the fusion of
Address, Contract and Binding. WCF formalizes this relationship in the form of an endpoint.Every service must have
Address that defines where the service resides, Contract that defines what the service does and a Binding that
defines how to communicate with the service

Administrative Endpoint Configuration

Configuring an endpoint administratively requires placing the endpoints in the hosting process' config file. For
example, given this service definition:

namespace endname
    interface IMyContract
    class MyService : IMyContract


                     <service name = "MyNamespace.MyService">
                         address = "http://localhost:8000/MyService/"
                         binding = "wsHttpBinding"
                         contract = "MyNamespace.IMyContract"

Multiple endpoints on the same service

<service name = "MyService">
   address = "http://localhost:8000/MyService/"
   binding = "wsHttpBinding"
   contract = "IMyContract"
   address = "net.tcp://localhost:8001/MyService/"
   binding = "netTcpBinding"
   contract = "IMyContract"
   address = "net.tcp://localhost:8002/MyService/"
   binding = "netTcpBinding"
   contract = "IMyOtherContract"

Service-side binding configuration

   <service name = "MyService">
      address = "net.tcp://localhost:8000/MyService/"
      bindingConfiguration = "TransactionalTCP"
      binding = "netTcpBinding"
       contract = "IMyContract"
       address = "net.tcp://localhost:8001/MyService/"
       bindingConfiguration = "TransactionalTCP"
       binding = "netTcpBinding"
       contract = "IMyOtherContract"
     <binding name = "TransactionalTCP"
       transactionFlow = "true"

Service-side programmatic endpoint configuration

ServiceHost host = new ServiceHost(typeof(MyService));

Binding wsBinding = new WSHttpBinding( );
Binding tcpBinding = new NetTcpBinding( );


host.Open( );


AddServiceEndpoint( )-It adds a service endpoint to the hosted service with a specified contract, binding, and
endpoint address.

To top