I have read many articles on WCF but I am always looking for an article which gives me a quick look at every topic of WCF. This article is for those who want to revise WCF in a single go. Let’s start with Service Oriented Architecture and after that, we will take a look at WCF.
What is SOA?
SOA is an architectural style for building business applications using loosely coupled services which are orchestrated to achieve a specific functionality by linking together. It’s not a technology or tool. SOA shifts our thinking from classes and objects to Services and Communication mechanisms to Messages instead of HTTP.
Points to Remember about SOA
- SOA separates application into services:
- It allows connectivity of dissimilar technologies
- It should be durable and secure
- Services and Messages are building blocks of SOA:
- Service-Self-contained business functionality
- Services should communicate using messages
- Service should define themselves
- Messages formats should be standard, e.g., XML
- Messages should be able to communicate asynchronously and should be reliable
- Messages should be able to describe and discover a service in a standard manner
Benefits of SOA
- Location independence
- Interoperable across platforms
- API-like development paradigm
- Better organisation than object distribution
- Loose coupling
Principles of Service Orientation
- Boundaries are explicit
- A service communicates by sending messages across their boundary
- All communication occurs through messages
- Services are autonomous
- We can build, manage, and version services independently
- We can change a service without affecting clients as long as clients can continue sending and receiving the same message
- Services share contracts and schema
- Contract describes the messages a service can send and receive
- Schemas define how the client and service construct the messages they exchange
What is the difference between a Service and a Component?
A Service is used to communicate in a heterogeneous environment. It enables us to go cross platform. Components and classes bind us to communicate inside a programming boundary. It ties us to a specific language.
Windows Communication Foundation
WCF was introduced in Microsoft .NET framework 3.0. Formerly known as Indigo, it’s a unified model that brings together web services, Remoting, Sockets, MSMQ and Enterprise services. It enforces the SOA approach and provides Transaction support, security, durability, etc.
All the classes related to WCF come under the
Benefits of WCF
- Instance Management
- Fault Isolation
- Error Masking
All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Endpoints provide clients access to the functionality offered by a WCF service – MSDN.
The host application makes the service available by providing one or more endpoints to the clients. Each endpoint consists of three elements: Address, Binding, and Contract.
Address: In WCF, every service is associated with a unique address. The address consists of two important elements:
- Service location – The portion of the address indicates the name of the target machine
- Transport protocol - Example: HTTP/HTTPS, TCP, IPC, MSMQ, etc.
[base address] / [optional URI]
Example: http:// localhost:8001
Contracts: The Contract is a platform-neutral and standard way of describing what the service does. WCF defines four types of Contracts:
- Service Contracts
- It is an interface that defines a service operation which works with a single type or data contracts.
- Describes which operations the client can perform on the service.
[ServiceContract] attribute marks an interface as a service contract.
[OperationContact] attribute exposes methods of the interface.
public interface IEmployeeService
String GetEmployeeName (int empId);
StudentInformation GetEmployeeInfo (int empId);
- Data Contracts
- Defines which data types are passed to and from the service.
- Describes how CLR types are mapped to XSD schema definitions.
- WCF defines implicit contracts for built-in-types such as
string, but you can easily define explicit opt-in-data contract for custom types.
- Enables complex types to be serialized and deserialized so it can be passed.
- It must be decorated with
- Every member of a Data Contract should be decorated with
public class EmployeeInfo
Public string EmpName;
- Fault Contracts
- Define which errors are raised by the service and how the service handles and propagates errors to its clients.
- We use the
FaultContact attribute on service’s operation.
- Use the
FaultException<T> class to return a strongly typed SOAP fault.
- Message Contracts
- It allows the service to interact directly with messages.
- It can be typed or untyped.
- Message contracts are useful in interoperability cases.
- Clients and services communicate by passing messages
- The mechanism that they use to communicate is a channel stack, which is made up of channels.
- Bindings specify how clients and WCF services communicate:
- Specifies how a client endpoint communicates with a service endpoint.
- Specifies the transport protocol, message-encoding, security, transaction, and reliability the client and service will use.
- A binding represents the channel stack.
- Bindings inherit from the
- Consists of binding elements describing some aspects of the communication.
- Transport (HTTP, TCP, etc.)
- Protocol (security, reliability, transaction, etc.)
- Message encoding (text, binary, etc.)
- A binding is comprised of several binding elements.
- Each binding element corresponds to a channel in the channel stack.
- Binding element inherits from the
- Can be created declaratively or programmatically.
The frequently used bindings in WCF are:
BasicHttpBinding – It is designed to expose a WCF service as a legacy ASMX web service.
WSHttpBinding- It uses HTTP or HTTPS for transport and offers a variety of features over internet, all using the WS-* standards. Visual Studio uses this by default.
WSDualHttpBinding – Support WS-* plus two way communication.
NetTcpBinding – TCP binding uses TCP for cross-machine communication on the intranet. It supports a variety of features including reliability, transactions, and security. It is optimized for WCF – to – WCF communication.
NetNamedPipeBinding – This binding is used for inter Process Communication (IPC). It uses named pipes as a transport for same machine communication. It is the most secure binding.
NetMsmqBinding- This binding uses MSMQ for transport and offers support for disconnected queued calls.
- It exposes service on wire so client can use it.
- A WCF service can be hosted in the following ways:
- IIS hosting
- Self hosting
- Console application
- Window Service
- Windows Forms or a WPF application
- WAS hosting
- Role of the host application
- Start and stop the service
- Listen for request from clients
- Direct those requests to the service
- Send responses back to the clients
ServiceHost object is associated with a WCF service at runtime.
- Binding configuration allows tweaking of communication characteristics between the client and the service.
- Sets the characteristics of the transport.
- Usually set on both the client and the service.
- Set on the endpoint itself.
- Potential settings:
- Session reliability
- Message size
- Can be used with
- Enables communication retries
- Also enforces message ordering
<reliableSession enabled="true" order="true"/>
b) Message size
- Can be used with any binding
- Increase or decrease total message size, default 64K
- Useful when returning lists or a stream
<binding name="binding1" maxReceivedMessageSize="100000">
c) Transmission Timeouts
- Can be used with any Binding
- Sets the maximum time WCF will wait for a message to be transmitted
- Throws exception when exceeded
- Default is 45
<binding name="binding1" sendTimeOut="00:01:00"
- Always set reliability to
- Benefits from retries
- Ordering may only be important for one way calls
- Default timeouts are usually fine
- Attribute controls Runtime aspects of the service
- Characteristics specify to the service only
- Does not affect binding (Transport)
- Typically unknown to client
- It can be configured in a number of ways
- Two types of settings (not always both)
- Attribute decoration (at Service class level)
- Service behaviour defined at Service level not at the endpoint level like Binding configuration.
<service name="MyService" behaviourConfiguration="behaviour1">
Public class MyService : IMyService
... implementation code here....
- By default, service does not pass error details to client.
- We can set attribute in config file or on service to get more clear error information.
- In config file:
Public class MyService : IMyService
... implementation code here....
Thanks for reading. if you like the article, please vote up. Happy coding!!!